簡體   English   中英

嘗試從 ASP.NET 頁面通過 SMTP 發送 email 時出現權限問題

[英]Permission issues when trying to send email via SMTP from ASP.NET page

我以前做過這個沒有任何問題,但現在我不知道出了什么問題。 我有一個 web 頁面,其中有一個 email 按鈕,我想將一些數據發送到 email 地址。

我向我們的 web 托管公司詢問了服務器詳細信息,我得到的答復是:

"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.
"

我不確定最后一行,但我在 web 主機的控制面板中創建了一個新的 email 地址。

我使用的代碼是:

// 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 - 我在控制面板中創建的 email 地址 emailPassword - 上面 Z0C83F57C786A0B4A39EFAB23731CEB7 的密碼

我遇到的錯誤是,當我向我們自己以外的其他域發送電子郵件時,我得到了

"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."

當我 email 到我們主機內的地址時,它工作正常。

支持不是很支持所以我在這里問你可能認為問題是什么? 我覺得奇怪的是我使用我創建的 email 地址的密碼,真的應該這樣嗎?

我認為您為NetworkCredential使用了錯誤的 email 地址。 它應該是您從提供emailServer的帳戶獲得的 email 帳戶。

嘗試這個..

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

這是發送郵件的代碼..我希望對您有所幫助..

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;
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM