2012-10-31 108 views

回答

0
const string HTML_TAG_PATTERN = "<.*?>"; 
static string StripHTML(string inputString) 
{ 
    return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty); 
} 

public static void sendMessage() 
{ 
    var username = "[email protected]"; 
    var password = "password"; 
    MailAddress MailFrom = new MailAddress("[email protected]"); 
    MailAddress MailTo = new MailAddress("[email protected]"); 
    var subject = "TEST SUBJECT"; 
    var attachmentPath = "test.pdf"; 
    var mailBody = "<b>test</b>"; 


    NetworkCredential cred = new NetworkCredential(username, password); 

    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.UseDefaultCredentials = false; 
    smtp.EnableSsl = true; 
    smtp.Credentials = cred; 
    smtp.Port = 587; 

    MailMessage mail = new MailMessage(); 

    mail.IsBodyHtml = true; 

    AlternateView avAlternateView = null; 
    Encoding myEncoding = Encoding.GetEncoding("UTF-8"); 

    avAlternateView = AlternateView.CreateAlternateViewFromString(StripHTML(mailBody), myEncoding, "text/plain"); 
    mail.AlternateViews.Add(avAlternateView); 

    avAlternateView = AlternateView.CreateAlternateViewFromString(mailBody, myEncoding, "text/html"); 
    mail.AlternateViews.Add(avAlternateView); 

    mail.Sender = MailFrom; 
    mail.From = MailFrom; 
    mail.ReplyTo = MailFrom; 

    mail.To.Add(MailTo); 

    mail.Subject = subject; 
    mail.SubjectEncoding = Encoding.GetEncoding("UTF-8"); 

    mail.BodyEncoding = Encoding.GetEncoding("UTF-8"); 

    Attachment attachment = new Attachment(attachmentPath); 
    mail.Attachments.Add(attachment); 
    try 
    { 
    smtp.Send(mail); 
    } 
    catch (Exception ex) 
    { 
    } 
} 
+0

工作就像一個魅力!非常感謝! – Aqua267