2013-02-07 33 views
2

我想用C#發送帶有郵件的pdf文件。我知道我可以發送郵件,但我不知道我怎麼能發送郵件用PDF文件:(如何用PDF文件發送郵件

比如我在文件夾C PDF文件:\檢驗.pdf

這裏是我的代碼:

private void SendEmail(string pdfpath,string firstname, string lastname, string title, string company, string mailfrom,string mailto) 
{ 
    try 
    { 
     MailMessage m = new MailMessage(); 
     System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(); 

     m.From = new System.Net.Mail.MailAddress(mailfrom); 
     m.To.Add(mailto); 

     m.Subject = "Company Gastzugang (" + lastname + ", " + firstname + ")"; 


     // what I must do for sending a pdf with this email 

     m.Body = "Gastzugangdaten sind im Anhang enthalten"; 

     sc.Host = SMTPSERVER; // here is the smt path 

     sc.Send(m); 
    } 
    catch (Exception ex) 
    { 
     error.Visible = true; 
     lblErrorMessage.Text = "Folgender Fehler ist aufgetreten: " + ex.Message; 
    } 
} 
+0

你需要它作爲附件? – ryadavilli

回答

4

你可以這樣做:

var filename = @"c:\test.pdf"; 
m.Attachments.Add(new Attachment(filename)); 
1
System.Net.Mail.Attachment attachment; 
attachment = new System.Net.Mail.Attachment("filename"); 
m.Attachments.Add(attachment); 
0

這裏是我的全部代碼:

public void SendMail() 
{ 
    MailMessage msg = new MailMessage(); 
    msg.From = new MailAddress("[email protected]"); 
    string s = txtEmail.Text; 
    msg.To.Add(txtEmail.Text); 
    msg.Body = "<html><body><img src='~/images/back.png'/><br></body></html>"; 
    msg.IsBodyHtml = true; 
    msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8"); 
    Attachment at = new Attachment(Server.MapPath("~/Main/images/English.pdf")); 
    //Dim at1 As New Attachment(Server.MapPath("~/Main/images/English.pdf")) 
    msg.Attachments.Add(at); 
    //msg.Attachments.Add(at1) 
    msg.Priority = MailPriority.High; 
    msg.Subject = "Special Gift"; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.EnableSsl = true; 
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "gmailpassword"); 
    smtp.Send(msg); 
} 
相關問題