开发者

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

开发者 https://www.devze.com 2023-03-02 14:38 出处:网络
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 w开发者_如何学编程ith.

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 w开发者_如何学编程ith.

I asked our web hosting company for server details and the response I got was:

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

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

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.

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?


I think that you are using the wrong email address for the NetworkCredential. It should be the one for your email account that you got from the one providing emailServer.


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

精彩评论

暂无评论...
验证码 换一张
取 消