如何发送电子邮件

时间:2013-05-30 13:08:22

标签: c# asp.net

我是Asp.net的新用户,我需要使用Asp.netOutlook发送电子邮件。 我在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);
}

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

5 个答案:

答案 0 :(得分:2)

首先获取公司的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);

答案 1 :(得分:1)

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

  • 发件人政策框架(SPF)
  • 白名单/黑名单
  • 验证
  • 邮件退回

自己很难做到这一点,这就是为什么你可能想考虑使用服务提供商。您只需使用他们的API(通常是REST调用),然后他们完成其余的工作。以下是三个这样的提供者:

Mandrill有一个低端免费计划,如果您在Windows Azure中使用它,SendGrid也是如此。即使对于较大的计划,它们也都是合理的价格。

我强烈建议您使用自己的API而不是自己使用System.Net.Mail。但是,如果您愿意,它们也可以充当您的SMTP中继,以便您可以使用其SMTP服务器并保持System.Net.Mail代码完好无损。

答案 2 :(得分:0)

您可以参考以下链接:

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

send email asp.net c#

我希望它会对你有所帮助。 :)

答案 3 :(得分:0)

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

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

}

答案 4 :(得分:0)

尝试亚马逊简单电子邮件服务(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);
        }