我以前这样做过,没有任何问题,但现在我不知道哪里出了问题。我有一个网页的电子邮件按钮,我想发送一些数据到电子邮件地址。
我向我们的虚拟主机公司询问了服务器的详细信息,得到的答复是:
"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主机的控制面板中创建了一个新的电子邮件地址。
我使用的代码是:
// 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."当我向我们主机内的地址发送电子邮件时,它工作得很好。
支持不是很支持,所以我在这里问你可能认为问题是什么?我发现我用我创建的电子邮件地址的密码很奇怪,真的应该这样吗?
发布于 2011-05-04 20:53:17
我认为您使用了错误的NetworkCredential电子邮件地址。它应该是您从提供emailServer的电子邮件帐户中获得的电子邮件帐户。
发布于 2011-05-04 21:08:20
试试这个..。
    msg.UseDefaultCredentials = false;
    NetworkCredential MyCredential = new NetworkCredential("Email", "Password");
    msg.Credentials = MyCredential;发布于 2011-05-04 22:27:30
下面是发送邮件的代码。我希望我能对你有所帮助。
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;
    }
}
}https://stackoverflow.com/questions/5883633
复制相似问题