2017-03-27 1 views
0

Ci-dessous est mon code par lequel j'essaye d'envoyer un email au destinataire. Je veux joindre un fichier .PDF qui est enregistré dans le dossier du projet. J'ai essayé d'utiliser:C#: Comment joindre un fichier PDF à un e-mail?

mail.Attachments.Add(new Attachment("c:\\temp\\example.txt")); 

mais cela ne fonctionne pas. Comment joindre un fichier?

de code C# échantillon

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace SendEmail 
{ 

public partial class SendEmail : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SendMail(); 
    } 

    protected void SendMail() 
    { 

     var fromAddress = ""; 
     var toAddress = ""; 
     //Password of your gmail address 
     const string fromPassword = ""; 
     string subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]"; 
     string body = "Your Incomplete Grade Application has been Result[]"; 

     // smtp settings 
     var smtp = new System.Net.Mail.SmtpClient(); 
     { 
      smtp.Host = "10.12.46.3"; 
      smtp.Port = 25; 
      smtp.EnableSsl = false; 
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
      smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
      smtp.Timeout = 20000; 
     } 
     // Passing values to smtp object 
     smtp.Send(fromAddress, toAddress, subject, body); 
    } 
} 
} 
+0

Quelles erreurs voyez-vous/est tout connecté? Pouvez-vous réduire le problème du tout? –

+0

Je ne vois pas d'erreur, j'envoie même un email mais il n'y a pas de pièce jointe, j'ai vérifié le chemin aussi et c'est correct. J'ai utilisé: MailMessage mail = new MailMessage(); Pièce jointe at = new Pièce jointe (Server.MapPath ("~/files/test.pdf")); mail.Attachments.Add (at); – Naive

+0

Où dans votre exemple de code va l'utilisation de l'objet 'MailMessage'? – Alejandro

Répondre

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Mail; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 

namespace SendEmail 
{ 
public partial class SendEmail : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     SendMail(); 
    } 
    protected void SendMail() 
    { 

     MailMessage mail = new MailMessage(); 

     mail.From = new MailAddress(""); 
     mail.To.Add(""); 
     mail.Subject = "INCOMPLETE APPLICATION CASE ID [CASE ID]"; 
     mail.Body = "Your Incomplete Grade Application has been Result[]"; 

     System.Net.Mail.Attachment attachment; 
     attachment = new System.Net.Mail.Attachment(Server.MapPath("files/test.pdf")); 
     mail.Attachments.Add(attachment); 
     var smtp = new System.Net.Mail.SmtpClient(); 
     { 
      smtp.Host = "10.12.46.3"; 
      smtp.Port = 25; 
      smtp.EnableSsl = false; 
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
      smtp.Credentials = new NetworkCredential("", ""); 
     } 
     smtp.Send(mail); 
    } 

} 

} 
1

Pas de temps pour les commentaires en ce moment, mais cela vous sortir de votre bind.

private void SendIt() 
    { 
     var fromAddress = ""; 
     var toAddress = ""; 
     //Password of your gmail address 
     const string fromPassword = ""; 
     // smtp settings 
     var smtp = new System.Net.Mail.SmtpClient(); 
     { 
      smtp.Host = ""; 
      smtp.Port = 25; 
      smtp.EnableSsl = false; 
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
      smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
      smtp.Timeout = 20000; 
     } 
     MailMessage msg = new MailMessage(); 
     MailAddress ma = new MailAddress(fromAddress); 
     msg.To.Add(toAddress); 
     msg.From = ma; 
     msg.Attachments.Add(new Attachment(@"C:\temp\myreport.log")); 
     msg.Body = "Your body message"; 
     msg.Subject = "Your subject line"; 
     smtp.Send(msg); 
    }