4

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.gmail.com"/>

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

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

  </appSettings>
Yahia
  • 69,653
  • 9
  • 115
  • 144
Neo
  • 51
  • 3
  • 5

7 Answers7

4

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.

Community
  • 1
  • 1
Nazim Ali
  • 180
  • 1
  • 13
  • A quick note, using the mailSettings that ScottE recommended you can replace the bulk of the code that is used to setup the SMTP Client. It makes re-configuration easier later when you need to change anything about how you're sending email. The settings for all of it are just there. – Nick Bork Feb 07 '12 at 20:24
1

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.

Brian Rogers
  • 142
  • 2
  • 9
1

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); 
Nick Bork
  • 4,831
  • 1
  • 24
  • 25
0

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;
}     
Matthias
  • 7,432
  • 6
  • 55
  • 88
Chetan S
  • 935
  • 1
  • 11
  • 21
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);
    }
}
Matthias
  • 7,432
  • 6
  • 55
  • 88
Surya R Praveen
  • 3,393
  • 1
  • 24
  • 25
0

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"
);
Matthias
  • 7,432
  • 6
  • 55
  • 88
user404
  • 256
  • 2
  • 9
  • I haven't seen this method used since CDO. The constructor for SmtpClient takes host and port. you can also specify the port using .Port = 25 – Nick Bork Feb 07 '12 at 20:18
0

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

StackOverflow - Send email with attachments

Gmail port = 465
Use SSL = true

Community
  • 1
  • 1
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39