2013-05-13 400 views
0

我在我的網站上有聯繫頁面,我希望用戶輸入他們的姓名和電子郵件ID(無密碼)並按下按鈕發送電子郵件。通過SMTP發送電子郵件

我做了谷歌和所有的解決方案我收到需要用戶密碼,

public static void SendMessage(string subject, string messageBody, string toAddress, string ccAddress, string fromAddress) 
{ 
    MailMessage message = new MailMessage(); 
    SmtpClient client = new SmtpClient(); 

    // Set the sender's address 
    message.From = new MailAddress(fromAddress); 

    // Allow multiple "To" addresses to be separated by a semi-colon 
    if (toAddress.Trim().Length > 0) 
    { 
     foreach (string addr in toAddress.Split(';')) 
     { 
      message.To.Add(new MailAddress(addr)); 
     } 
    } 
    // Allow multiple "Cc" addresses to be separated by a semi-colon 
    if (ccAddress.Trim().Length > 0) 
    { 
     foreach (string addr in ccAddress.Split(';')) 
     { 
      message.CC.Add(new MailAddress(addr)); 
     } 
    } 
    // Set the subject and message body text 
    message.Subject = subject; 
    message.Body = messageBody;  

    // smtp settings 
    var smtp = new System.Net.Mail.SmtpClient(); 
    { 
     smtp.Host = "smtpHost"; 
     smtp.Port =portno; 
     smtp.EnableSsl = true; 
     smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
     smtp.Credentials = new NetworkCredential("fromEmail", "fromPassword"); 
     smtp.Timeout = 20000; 
    } 

    // Send the e-mail message 
    smtp.Send(message); 
} 

但我不希望強迫用戶輸入密碼。有沒有其他解決方案使用SMTP?

我得到的錯誤是A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

+2

您似乎意外地寫了一個網絡釣魚程序。你不應該詢問用戶密碼。很可能您需要獲取SMTP服務器的密碼,因爲匿名發送通常是鎖定的。這將是您控制的密碼,而不是您的用戶。 – 2013-05-13 17:56:46

+0

是的......這是合乎邏輯的......我也嘗試過,但我收到錯誤信息爲「發送電子郵件失敗」。我確定用戶名和密碼正確 – user1181942 2013-05-13 18:07:35

+0

InnerException屬性將幫助您解決此問題。 – 2013-05-13 18:11:03

回答

0

我有我的實用工具類此功能,它就像一個魅力。

public static bool SendMail(string Email, string MailSubject, string MailBody) 
    { 
     bool isSent = false, isMailVIASSL = Convert.ToBoolean(ConfigurationManager.AppSettings["MailServerUseSsl"]); 

     string mailHost = ConfigurationManager.AppSettings["MailServerAddress"].ToString(), 
     senderAddress = ConfigurationManager.AppSettings["MailServerSenderUserName"].ToString(), 
     senderPassword = ConfigurationManager.AppSettings["MailServerSenderPassword"].ToString(); 

     int serverPort = Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"]); 

     MailMessage msgEmail = new MailMessage(new MailAddress(senderAddress), new MailAddress(Email)); 
     using (msgEmail) 
     { 
      msgEmail.IsBodyHtml = true; 
      msgEmail.BodyEncoding = System.Text.Encoding.UTF8; 
      msgEmail.Subject = MailSubject; 
      msgEmail.Body = MailBody; 

      using (SmtpClient smtp = new SmtpClient(mailHost)) 
      { 
       smtp.UseDefaultCredentials = false; 
       smtp.EnableSsl = isMailVIASSL; 
       smtp.Credentials = new NetworkCredential(senderAddress, senderPassword); 
       smtp.Port = serverPort; 
       try 
       { 
        smtp.Send(msgEmail); 
        isSent = true; 
       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 
      } 
     } 
     return isSent; 
    } 

<!-- Mail Server Settings --> 
<add key="MailServerAddress" value="smtp.gmail.com" /> 
<add key="MailServerPort" value="25" /> 
<add key="MailServerSenderUserName" value="[email protected]" /> 
<add key="MailServerSenderPassword" value="password" /> 
<add key="MailServerUseSsl" value="True" /> 
+0

嘿謝謝@Ali Umair,但你的代碼需要「發件人地址」和「發件人密碼」,我不想要 – user1181942 2013-05-18 09:19:42

+0

這些是你的用戶名和密碼爲你的帳戶,將semd電子郵件通常[email protected]。這些不是用戶的電子郵件和密碼,而參數中的電子郵件是用戶的電子郵件地址。 – 2013-05-20 07:53:20

+0

是的,我嘗試了相同的但沒有用..似乎有其他問題.. – user1181942 2013-05-20 08:15:26

相關問題