2011-09-08 230 views
1

地獄夥計!使用SMTP服務器發送郵件

我已經使用SMTP服務器發送郵件按以下

public bool SendMail(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, string attachment, bool isBodyHtml) 
{ 
    bool SendStatus = false; 
    System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage(mailFrom, mailTo); 

    if (mailCC != string.Empty) 
     mailMesg.CC.Add(mailCC); 

    if (mailBCC != string.Empty) 
     mailMesg.Bcc.Add(mailBCC); 

    if (!string.IsNullOrEmpty(attachment)) 
    { 
     System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachment); 
     mailMesg.Attachments.Add(attach); 
    } 
    mailMesg.Subject = subject; 
    mailMesg.Body = body; 
    mailMesg.IsBodyHtml = isBodyHtml; 
    mailMesg.ReplyTo = new System.Net.Mail.MailAddress(mailFrom); 

    System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient(); 


    string Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString(); 
    string UserName = System.Configuration.ConfigurationManager.AppSettings["MailUserId"].ToString(); 
    string password = System.Configuration.ConfigurationManager.AppSettings["MailPassword"].ToString(); 


    objSMTP.Host = Host; 
    objSMTP.Port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString()); 
    objSMTP.Credentials = new System.Net.NetworkCredential(UserName, password); 


    objSMTP.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 

    try 
    { 
     objSMTP.Send(mailMesg); 
     SendStatus = true; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     mailMesg.Dispose(); 
     mailMesg = null; 
    } 


    return SendStatus; 

} 

我想知道,是否可以發送郵件,而用戶名密碼&? 如果可能的話,任何人都可以請建議我該怎麼做?

回答

0

當然,如果你的smtp服務器不需要cridentials,你不應該指定em。否則,你應該。

0

我認爲你需要白名單的IP,你將發送電子郵件從哪個將是你的服務器,這個選項是完成的郵件帳戶,你要發送電子郵件。

相關問題