2016-06-05 2 views
-2

Ich entwickle ein ASP.NET MVC-Projekt. In meinem Projekt muss ich eine E-Mail mit Anhang senden. Aber wenn ich eine E-Mail sende, wird die E-Mail erfolgreich gesendet, aber der Anhang ist nicht enthalten.Senden von E-Mail mit Anhang in ASP.NET MVC funktioniert nicht

Dies ist meine E-Mail senden Methode

public bool Send(string email, string subject, string body, HttpPostedFileBase fileUploader, bool html = true) 
{ 
    try 
    { 
     string from = AppEmail; //example:- [email protected] 

     using (MailMessage mail = new MailMessage(from, email)) 
     { 

      mail.Subject = subject; 

      mail.Body = body; 

      if (fileUploader != null && fileUploader.ContentLength>0) 
      { 

       string fileName = Path.GetFileName(fileUploader.FileName); 

       mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName)); 
      } 

      mail.IsBodyHtml = html; 

      SmtpClient smtp = new SmtpClient(); 

      smtp.Host = "smtp.gmail.com"; 

      smtp.EnableSsl = true; 

      NetworkCredential networkCredential = new NetworkCredential(from, EmailPassword); 

      smtp.UseDefaultCredentials = true; 

      smtp.Credentials = networkCredential; 

      smtp.Port = 587; 

      smtp.Send(mail); 

      return true; 
     } 
    } 
    catch 
    { 
     return false; 
    } 
} 

Also bitte, warum Befestigung nicht enthalten ist? Wie kann ich eine E-Mail mit Anhang senden?

+2

Legen Sie Haltepunkte fest, gehen Sie durch Ihren Code. Wird der Anhang tatsächlich hinzugefügt? – CodeCaster

Antwort

-1

Sie diese Lösung verwenden können:

erstellen diese Klasse

public class Attachment 
{ 
    public string Path { get; set; } 
    public string Name { get; set; } 
    public string ContentType { get; set; } 
} 

dann diese Schnittstelle erstellen

public interface IFileAttachmentService 
{ 
    Attachments Save(HttpPostedFileBase file); 
} 

dann implementieren, um die Schnittstelle zu dieser Klasse

public class FileAttachmentService : IFileAttachmentService 
{ 
    private static readonly string _folder = "~/Attachments/"; 

    public Attachments Save(HttpPostedFileBase file) 
    { 
     if (file == null) 
      return new Attachments(); 

     var savePath = GenerateUniqueFileName(_folder + Path.GetFileName(file.FileName)); 

     file.SaveAs(HttpContext.Current.Server.MapPath(savePath)); 
     return new Attachments 
     { 
      ContentType = file.ContentType, 
      Name = Path.GetFileName(file.FileName), 
      Path = savePath 
     }; 
    } 

    private string GenerateUniqueFileName(string basedOn) 
    { 
     if (string.IsNullOrWhiteSpace(basedOn)) 
      throw new ArgumentNullException("basedOn"); 

     return (Path.GetDirectoryName(basedOn) 
      + "\\" 
      + Path.GetFileNameWithoutExtension(basedOn) 
      + "." 
      + Path.GetRandomFileName() 
      + Path.GetExtension(basedOn)) 
      .Replace('\\', '/'); 
    } 
} 

, dann erstellen Sie diese Methode

public static void SendEmail(string to, string toName, string subject, string body, string attachment) 
{ 
    var fromAddress = new MailAddress("Your Gemail Address", "Sender Project System Name"); 
    var toAddress = new MailAddress(to, toName); 
    var att = new Attachment(HttpContext.Current.Server.MapPath(attachment)); 
    const string fromPassword = "Your pass"; 

    var smtp = new SmtpClient 
    { 
     Host = "smtp.gmail.com", 
     Port = 587, 
     EnableSsl = true, 
     DeliveryMethod = SmtpDeliveryMethod.Network, 
     UseDefaultCredentials = false, 
     Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
    }; 
    using (var message = new MailMessage(fromAddress, toAddress) 
    { 
     Subject = subject, 
     Body = body 
    }) 
    { 
     message.Attachments.Add(att); 
     smtp.Send(message); 
    } 
} 

vorsichtig seine Anhangdatei in dem richtigen Modus setzen, wenn Sie nicht mit angehängter Datei Überlastung diese Methode ohne Ansetzparameter senden müssen.

Verwandte Themen