2011-05-04 12 views
0

我之前完成了此操作,沒有任何問題,但現在我不知道什麼是錯誤的。我有一個網頁,上面有一個電子郵件按鈕,我想用它將一些數據發送到電子郵件地址。嘗試通過SMTP從ASP.NET發送電子郵件時的權限問題頁面

我問我們的網絡託管公司的服務器的詳細信息,我得到的迴應是:

"You can use the following details for mail. 

Incoming mail server: mail.ourSite.com Outgoing mail server: mail.ourSite.com 

Username and password are the email address and password associated with the email address. 
" 

我不知道,最後一行,但我創建的虛擬主機控制面板新的電子郵件地址。

我使用的代碼是:

// instantiate a new mail definition and load an html 
// template into a string which I replace values in 
// then the rest of the code below 
md.Subject = String.Format("{0} {1} {2}", emailSubject, firstName, lastName); 

MailMessage msg = md.CreateMailMessage(emailAddress, replacements, emailBody, new Control()); 
md.IsBodyHtml = true; 
SmtpClient sc = new SmtpClient(emailServer);    
sc.Credentials = new NetworkCredential(emailUsername, emailPassword); 
    try 
    { 
     sc.Send(msg); 
    } 

emailserver的 - mail.ourSite.com(在這個崗位虛值) emailUsername - 的密碼 - 我在控制面板 emailPassword創建的電子郵件地址電子郵件以上

我的錯誤是,當我發送電子郵件至其他領域比我們自己,我得到

"Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server." 

當我通過電子郵件發送到我們的主機內的地址,然後它工作正常。

支持不是很支持,所以我在這裏問你可能會認爲問題可能是什麼?我覺得奇怪的是,我使用密碼來創建我創建的電子郵件地址,是否應該這樣做?

回答

0

我認爲您使用的電子郵件地址爲NetworkCredential。它應該是您提供emailServer的電子郵件帳戶中的一個。

0

試試這個..

msg.UseDefaultCredentials = false; 
    NetworkCredential MyCredential = new NetworkCredential("Email", "Password"); 
    msg.Credentials = MyCredential; 
+0

也許你正在使用錯誤的電子郵件地址和密碼。 。 – 2011-05-04 13:13:04

0

這裏是代碼發送郵件.. 我希望能對你有所幫助..

using System.Web.Mail; 
using System; 
public class MailSender 
{ 
    public static bool SendEmail(
     string pGmailEmail, 
     string pGmailPassword, 
     string pTo, 
     string pSubject, 
     string pBody, 
     System.Web.Mail.MailFormat pFormat, 
     string pAttachmentPath) 
    { 
    try 
    { 
     System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage(); 
     myMail.Fields.Add 
      ("http://schemas.microsoft.com/cdo/configuration/smtpserver", 
          "smtp.gmail.com"); 
     myMail.Fields.Add 
      ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 
          "465"); 
     myMail.Fields.Add 
      ("http://schemas.microsoft.com/cdo/configuration/sendusing", 
          "2"); 
     //sendusing: cdoSendUsingPort, value 2, for sending the message using 
     //the network. 

     //smtpauthenticate: Specifies the mechanism used when authenticating 
     //to an SMTP 
     //service over the network. Possible values are: 
     //- cdoAnonymous, value 0. Do not authenticate. 
     //- cdoBasic, value 1. Use basic clear-text authentication. 
     //When using this option you have to provide the user name and password 
     //through the sendusername and sendpassword fields. 
     //- cdoNTLM, value 2. The current process security context is used to 
     // authenticate with the service. 
     myMail.Fields.Add 
     ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1"); 
     //Use 0 for anonymous 
     myMail.Fields.Add 
     ("http://schemas.microsoft.com/cdo/configuration/sendusername", 
      pGmailEmail); 
     myMail.Fields.Add 
     ("http://schemas.microsoft.com/cdo/configuration/sendpassword", 
      pGmailPassword); 
     myMail.Fields.Add 
     ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", 
      "true"); 
     myMail.From = pGmailEmail; 
     myMail.To = pTo; 
     myMail.Subject = pSubject; 
     myMail.BodyFormat = pFormat; 
     myMail.Body = pBody; 
     if (pAttachmentPath.Trim() != "") 
     { 
      MailAttachment MyAttachment = 
        new MailAttachment(pAttachmentPath); 
      myMail.Attachments.Add(MyAttachment); 
      myMail.Priority = System.Web.Mail.MailPriority.High; 
     } 

     System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465"; 
     System.Web.Mail.SmtpMail.Send(myMail); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     throw; 
    } 
} 
} 
相關問題