简体   繁体   English

尝试从 ASP.NET 页面通过 SMTP 发送 email 时出现权限问题

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

I have done this before without any issue but now I don't know what's wrong.我以前做过这个没有任何问题,但现在我不知道出了什么问题。 I have a web page with a button for email which I want to send some data to email addresses with.我有一个 web 页面,其中有一个 email 按钮,我想将一些数据发送到 email 地址。

I asked our web hosting company for server details and the response I got was:我向我们的 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.
"

I am not sure about the last line but I created a new email address in the web host's control panel.我不确定最后一行,但我在 web 主机的控制面板中创建了一个新的 email 地址。

The code I use is:我使用的代码是:

// 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 (dummy value in this post) emailUsername - the email address I created in the control panel emailPassword - the password for the email above emailServer - mail.ourSite.com(这篇文章中的虚拟值) emailUsername - 我在控制面板中创建的 email 地址 emailPassword - 上面 Z0C83F57C786A0B4A39EFAB23731CEB7 的密码

The error I have is that when I send emails to other domains than our own I get我遇到的错误是,当我向我们自己以外的其他域发送电子邮件时,我得到了

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

When I email to an address within our host then it works fine.当我 email 到我们主机内的地址时,它工作正常。

The support is not very supportive so I am asking here what you might think the problem could be?支持不是很支持所以我在这里问你可能认为问题是什么? I find it strange that I use the password for an email address I created, should it really be like that?我觉得奇怪的是我使用我创建的 email 地址的密码,真的应该这样吗?

I think that you are using the wrong email address for the NetworkCredential .我认为您为NetworkCredential使用了错误的 email 地址。 It should be the one for your email account that you got from the one providing emailServer .它应该是您从提供emailServer的帐户获得的 email 帐户。

Try this..尝试这个..

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

here is code to send mail.. i hope i will helpful to you..这是发送邮件的代码..我希望对您有所帮助..

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