0

我是新来的Asp.net,我需要Asp.net使用我的Outlook. 我在 asp 中有一个按钮,当我单击按钮(发送)时,我想发送电子邮件。我尝试使用 Hotmail,Gmail但远程服务器被阻止。如果你不明白我的问题,请告诉我。我试过这个:

         var smtpClient = new SmtpClient
        {
            Host = "outlook.mycompany.local",
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("myEmail@mycommpany.com", "myPassword")
        };

        var message = new System.Net.Mail.MailMessage
        {
            Subject = "Test Subject",
            Body = "FOLLOW THE WHITE RABBIT",
            IsBodyHtml = true,
            From = new MailAddress("myemail@mycommapny.com")
        };
        // you can add multiple email addresses here
        message.To.Add(new MailAddress("friendEmail@Company.com"));

        // and here you're actually sending the message
        smtpClient.Send(message);
}

Exeption Show:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated 请问我该怎么做?

4

5 回答 5

1

从 ASP.net 网站发送出站电子邮件可能会出现问题。即使你得到了正确的 SMTP 信息,你仍然需要处理:

  • 发件人策略框架 (SPF)
  • 白名单/黑名单
  • 验证
  • 反弹

自己很难做到这一点,这就是为什么您可能要考虑改用服务提供商的原因。您只需使用他们的 API(通常是一个 REST 调用),剩下的就交给他们了。以下是三个这样的提供者:

Mandrill 有一个低端免费计划,如果您将它与 Windows Azure 一起使用,SendGrid 也是如此。即使对于较大的计划,它们都可以负担得起。

我强烈建议将其中之一与他们自己的 API 一起使用,而不是System.Net.Mail自己使用。但是,如果您愿意,他们也可以为您充当 SMTP 中继,这样您就可以使用他们的 SMTP 服务器并保持您的System.Net.Mail代码完整无缺。

于 2013-05-30T13:27:25.360 回答
1

首先获取公司 SMTP 服务器设置(我猜是从您的系统管理员那里),然后您可以执行以下操作:

// setting up the server
var smtpClient = new SmtpClient
{
    Host = "your.company.smtp.server",
    UseDefaultCredentials = false,
    EnableSsl = true, // <-- see if you need this
    Credentials = new NetworkCredential("account_to_use", "password")
};

var message = new MailMessage
{
    Subject = "Test Subject",
    Body = "FOLLOW THE WHITE RABBIT",
    IsBodyHtml = true,
    From = new MailAddress("from@company.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("neo@matrix.com"));

// and here you're actually sending the message
smtpClient.Send(message);
于 2013-05-30T13:27:50.243 回答
0

试试 Amazon Simple Email Service ( http://aws.amazon.com/ses/ )。如果您是 Amazon Web Services (AWS) 的新手,可能会有一个学习曲线。但是,一旦您熟悉了他们可以在 Nuget (AWSSDK) 上找到的 SDK,这个过程就非常简单(亚马逊确实有很多可能很古怪的小包装类)。

因此,要回答“如何发送电子邮件?”这个问题,它看起来像:

        var fromAddress = "from@youraddress.com";

        var toAddresses = new Amazon.SimpleEmail.Model.Destination("someone@somedestination.com");

        var subject = new Amazon.SimpleEmail.Model.Content("Message");

        var body= new Body(new Amazon.SimpleEmail.Model.Content("Body"));

        var message = new Message(subject , body);

        var client = ConfigUtility.AmazonSimpleEmailServiceClient;

        var request= new Amazon.SimpleEmail.Model.SendEmailRequest();

        request.WithSource(fromAddress)
                            .WithDestination(toAddresses)
                            .WithMessage(message );
        try
        {
            client.SendEmail(request);
        }
        catch (Amazon.SimpleEmail.AmazonSimpleEmailServiceException sesError)
        {
            throw new SupplyitException("There was a problem sending your email", sesError);
        }
于 2013-05-30T14:04:24.190 回答
0

你可以使用这个功能。还有一件事您必须将电子邮件 smtp 登录名和密码存储在 Web 配置文件中

/// <summary>
/// Send Email 
/// </summary>
/// <param name="strFrom"></param>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strAttachmentPath"></param>
/// <param name="IsBodyHTML"></param>
/// <returns></returns>
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
    Array arrToArray;
    char[] splitter = { ';' };
    arrToArray = strTo.Split(splitter);
    MailMessage mm = new MailMessage();
    mm.From = new MailAddress(strFrom);
    mm.Subject = strSubject;
    mm.Body = strBody;
    mm.IsBodyHtml = IsBodyHTML;
    //mm.ReplyTo = new MailAddress("replyto@xyz.com");
    foreach (string s in arrToArray)
    {
        mm.To.Add(new MailAddress(s));
    }
    if (strAttachmentPath != "")
    {
        try
        {
            //Add Attachment
            Attachment attachFile = new Attachment(strAttachmentPath);
            mm.Attachments.Add(attachFile);
        }
        catch { }
    }
    SmtpClient smtp = new SmtpClient();
    try
    {
        smtp.Host = ConfigurationManager.AppSettings["MailServer"].ToString();
        smtp.EnableSsl = true; //Depending on server SSL Settings true/false
        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
        NetworkCred.UserName = ConfigurationManager.AppSettings["MailUserName"].ToString();
        NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"].ToString();
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;//Specify your port No;
        smtp.Send(mm);
        return true;
    }
    catch
    {
        mm.Dispose();
        smtp = null;
        return false;
    }

}
于 2013-05-30T13:25:07.920 回答
-1

您可以参考以下链接:

http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

发送电子邮件 asp.net c#

我希望它会帮助你。:)

于 2013-05-30T13:14:48.303 回答