使用C#asp.net发送邮件

时间:2013-01-24 05:02:22

标签: c# asp.net

  

可能重复:
  send email asp.net c#

我之前已多次使用该技术发送邮件,但不知何故它无法正常工作我提供以下代码:

MailMessage myMailMessage = new MailMessage();
            myMailMessage.Subject = "Response From Client";
            myMailMessage.Body = "hello word";
            myMailMessage.From = new MailAddress("mymail@gmail.com", "jub");
            myMailMessage.To.Add(new MailAddress("mymail@gmail.com", "Softden"));

            SmtpClient mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMailMessage);

我的web.config是:

<mailSettings>
      <smtp deliveryMethod = "Network" from="Jubair &lt;mymail@gmail.com&gt;">
        <network defaultCredentials="false" enableSsl="true" userName="mymail@gmail.com" password="Mypassword" host="smtp.gmail.com" port="587"></network>
      </smtp>
    </mailSettings>

它说smtp服务器需要安全连接或客户端未经过身份验证..请帮助

4 个答案:

答案 0 :(得分:4)

尝试添加类似的内容 Per Dominic对Stackoverflow的回答看看他的例子

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               //UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

// -----------------以下简单方法--------------- 我刚刚在下面进行了测试,它可以正常工作

var mail = new MailMessage();

// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("noreplyXYZ@gmail.com");
mail.To.Add(new MailAddress(to));

// Define the message
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = message;

// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";//ForGmail
mailclient.Port = 587; //ForGmail


// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;//ForGmail
//mailclient.EnableSsl = false;
mailclient.UseDefaultCredentials = true;

// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential("fromAddress@gmail.com", "xxxx123");//ForGmail
mailclient.Send(mail);
mailclient.Dispose();

// .config设置有两个选项可供你如何设置我怀疑这是你遇到的问题

<system.net>
    <mailSettings>
      <smtp from="from@gmail.com" deliveryMethod="Network">
        <network userName="from@gmail.com" password="mypassword" host="smtp.gmail.com" port="587"/>
      </smtp>             
    </mailSettings>
  </system.net>

或选项2

<configuration>
    <appSettings>
        <add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
        <add key="portNumber" value="587"/>
        <add key="fromAddress" value="yourEmailAddress@gmail.com"/>
    </appSettings>
</configuration>

答案 1 :(得分:0)

从Yahoo,Gmail,Hotmail(C#)发送电子邮件
http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-Gmail-Hotmail-Csharp

答案 2 :(得分:0)

这些是带有示例的很好的教程。制作您的演示电子邮件ID。传递您的ID和密码作为参数。并向任何人发送邮件。

http://www.codeproject.com/Articles/15807/Easy-SMTP-Mail-Using-ASP-NET-2-0

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

http://www.codeproject.com/Tips/490604/Sending-mail-using-ASP-NET-with-optional-parameter

如果您仍然无法发送电子邮件,请务必更改端口号。但587应该可以正常工作。

答案 3 :(得分:0)

确保您与电子邮件服务器端联系,以了解他们接受哪种身份验证进行中继。