开发者

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

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

<appSettings>

    <add key="SMTP" value="smtp.gm开发者_JS百科ail.com"/>

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

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

  </appSettings>


Sending email in .NET through Gmail This link have a full code of sending email and its proper working sending email by gmailin my pc.


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.

 <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); 


Port 25 is the default port but not the correct port for sending email over 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

I think the address is not valid because it targets port 25 in the context of an SSL connection.


I did this already and I have already tested it out in Gmail.

StackOverflow - Send email with attachments

Gmail port = 465
Use SSL = true


You cant specify port 25 with :25 after the IP.

It will default to port 25 so you don't need it. If you want to change the port use the following:

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


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜