[英]send email from asp.net app
i configure all setting for send email using c# but when i execute than i get the following error The requested address is not valid in its context 74.125.53.109:25 我使用c#配置发送电子邮件的所有设置但是当我执行时我得到以下错误请求的地址在其上下文中无效74.125.53.109:25
my code is 我的代码是
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 Web.Config中
<appSettings>
<add key="SMTP" value="smtp.gmail.com"/>
<add key="FROMEMAIL" value="mail@gmail.com"/>
<add key="FROMPWD" value="password"/>
</appSettings>
通过Gmail在.NET中发送电子邮件此链接有一个完整的代码,用于通过我的电脑中的gmail发送电子邮件及其正常工作的电子邮件 。
Port 25 is the default port but not the correct port for sending email over SSL. 端口25是默认端口,但不是通过SSL发送电子邮件的正确端口。 Since you are using SSL, you will need to set
smtp.Port = 465
, according to Google's help page on the topic: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287 由于您使用的是SSL,因此需要设置
smtp.Port = 465
,根据Google主题的帮助页面: http : //support.google.com/mail/bin/answer.py? smtp.Port = 465
I think the address is not valid because it targets port 25 in the context of an SSL connection. 我认为该地址无效,因为它在SSL连接的上下文中以端口25为目标。
I figure I'll just post the combined effort of posts here: 我想我会在这里发布帖子的综合努力:
Add this to your configuration file, changing the email/username/password. 将其添加到配置文件中,更改电子邮件/用户名/密码。 You may have to change the port based on what Brian Rogers posted.
您可能必须根据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>
Use this in your code 在代码中使用它
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);
This is the function which works well for send mail, i had checked it and it's working. 这是一个适用于发送邮件的功能,我检查了它并且它正在工作。
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;
}
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);
}
}
You cant specify port 25 with :25
after the IP. 您无法在IP之后将端口25指定为
:25
。
It will default to port 25 so you don't need it. 它将默认为端口25,因此您不需要它。 If you want to change the port use the following:
如果要更改端口,请使用以下命令:
mail.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"portnumber"
);
I did this already and I have already tested it out in Gmail. 我已经这样做了,我已经在Gmail中测试了它。
StackOverflow - Send email with attachments StackOverflow - 发送带附件的电子邮件
Gmail port = 465 Gmail端口= 465
Use SSL = true 使用SSL = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.