从asp.net应用程序发送电子邮件

时间:2010-08-11 12:09:02

标签: c# asp.net

我使用c#配置发送电子邮件的所有设置,但是当我执行时,我得到以下错误 请求的地址在其上下文74.125.53.109:25

中无效

我的代码是

MailMessage mail = new MailMessage();
mail.To.Add("to@gmail.com");
mail.From = new MailAddress("from@gmail.com");
mail.Subject = "Test Email";

string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;   
smtp.Send(mail);

的Web.Config

<appSettings>

    <add key="SMTP" value="smtp.gmail.com"/>

    <add key="FROMEMAIL" value="mail@gmail.com"/>

    <add key="FROMPWD" value="password"/>

  </appSettings>

7 个答案:

答案 0 :(得分:4)

Sending email in .NET through Gmail 此链接具有发送电子邮件及其正常工作的完整代码  sending email by gmail在我的电脑里。

答案 1 :(得分:1)

端口25是默认端口,但不是通过SSL发送电子邮件的正确端口。由于您使用的是SSL,因此需要设置smtp.Port = 465,根据Google关于主题的帮助页面:http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

我认为该地址无效,因为它在SSL连接的上下文中以端口25为目标。

答案 2 :(得分:1)

我想我会在这里发布帖子的综合努力:

将此添加到您的配置文件中,更改电子邮件/用户名/密码。您可能必须根据Brian Rogers发布的内容更改端口。

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

在您的代码

中使用此功能
 MailMessage mail = new MailMessage(); 
 mail.To.Add("to@gmail.com"); 
 mail.From = new MailAddress("from@gmail.com"); 
 mail.Subject = "Test Email"; 

 string Body = "<b>Welcome to CodeDigest.Com!!</b>"; 
 mail.Body = Body; 
 mail.IsBodyHtml = true; 

 SmtpClient smtp = new SmtpClient();
 smtp.Send(mail); 

答案 3 :(得分:0)

您无法在IP之后使用:25指定端口25。

它将默认为端口25,因此您不需要它。如果要更改端口,请使用以下命令:

mail.Fields.Add(
    "http://schemas.microsoft.com/cdo/configuration/smtpserverport",
    "portnumber"
);

答案 4 :(得分:0)

我已经这样做了,我已经在Gmail中对其进行了测试。

StackOverflow - Send email with attachments

Gmail端口= 465
使用SSL = true

答案 5 :(得分:0)

这是一个适用于发送邮件的功能,我检查了它并且它正在工作。

private static bool testsendemail(MailMessage message)
{
    try
    {
        MailMessage message1 = new MailMessage();
        SmtpClient smtpClient = new SmtpClient();

        MailAddress fromAddress = new MailAddress("FromMail@Test.com");
        message1.From = fromAddress;
        message1.To.Add("ToMail@Test1.com");
        message1.Subject = "This is Test mail";
        message1.IsBodyHtml = true;
        message1.Body ="You can write your body here" + message;
        // We use yahoo as our smtp client
        smtpClient.Host = "smtp.mail.yahoo.com";
        smtpClient.Port = 587;
        smtpClient.EnableSsl = false;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential(
            "SenderMail@yahoo.com",
            "YourPassword"
        );

        smtpClient.Send(message1);
    }
    catch
    {
        return false;
    }
    return true;
}     

答案 6 :(得分:0)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{

    protected void btnSend_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = new MailAddress("xxx@yourdomain.com");
        msg.To.Add(txtTo.Text); //Text Box for To Address
        msg.Subject = txtSubject.Text; //Text Box for subject
        msg.IsBodyHtml = true;
        msg.Body = txtBody.Text; //Text Box for body
        msg.Priority = MailPriority.High;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(
            "relay-hosting.secureserver.net",
            25
        );
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(
            "xxx@yourdomain.com",
            "yourpassword"
        );
        client.Host = "relay-hosting.secureserver.net";
        client.EnableSsl = false;
        object userstate = msg;
        client.Send(msg);
    }
}