I use the following code to send email via Gmail , it works well when I use client.Port = 587, but it doesn't work if I use client.Port = 465 ! but If I use email client software such as Outlook Express with port 465, it woks well, I don't know if there are bugs in .NET.
protected void btnSend_Click(object sender, EventArgs e)
{
//Add Async="true" to .ASPX pageSmtpClient client=new SmtpClient();
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("my@gmail.com", "***");
client.Host = "smtp.gmail.com";
client.Port = 587;// client.Port = 465 don't work!
SendEmail("my@gmail.com", "To@Yahoo.com", "Subject 587", "Body 587", false, null, client);
}
/***************************** Send Email***********************/
#region Send Emailpublic static void SendEmail(
string From,
string To,
string Subject,
string Body,
bool IsBodyHtml,
string Attachment,
SmtpClient client)
{
if (IsValidEmail(From) && IsValidEmail(To))
{
DelegateSendEmail myDelegateSendEmail = new DelegateSendEmail(SyncSendEmail);
myDelegateSendEmail.BeginInvoke(From, To, Subject, Body, IsBodyHtml, Attachment, client, null, null);
}
}
private delegate void DelegateSendEmail(
string From,
string To,
string Subject,
string Body,
bool IsBodyHtml,
string Attachment,
SmtpClient client);
private static void SyncSendEmail(
string From,
string To,
string Subject,
string Body,
bool IsBodyHtml,
string Attachment,
SmtpClient client)
{
MailMessage message = new MailMessage(new MailAddress(From), new MailAddress(To));
message.Subject = Subject;
message.Body = Body;
message.IsBodyHtml = IsBodyHtml;
if (!string.IsNullOrEmpty(Attachment))
{
message.Attachments.Add(new Attachment(Attachment));
}client.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
try
{
client.SendAsync(message, message);
}
catch
{
}
}
private static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
{}
private static bool IsValidEmail(string strIn)
{
if (string.IsNullOrEmpty(strIn))
{
return false;
}
else
{
return Regex.IsMatch(strIn, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
}#endregion
/***************************** Send Email***********************/
SuperCool Multiple ZIP - A utility to unzip multiple files and work with multiple zip files
SuperCool Random Number Generator
![]() |
0 |
![]() |
I don't think this is a problem w/ .NET. It could be that your network is blocking connection to port 465. What error are you getting?
If you have access to the server, I would try telnet to smtp.gmail.com over port 465.
Bruce
DiscountASP.NET: Developer Ready ASP.NET Web Hosting
- Microsoft Gold Certified Partner
- Voted 2008, 2007, 2006 & 2005 Best ASP.NET Web Hosting by asp.netPRO Magazine
![]() |
0 |
![]() |
itrs not probblem with .net in the web.config file add mail settings or in the appsettings
add the smtpserver and value
and check the relay address in the IIS Settings.
![]() |
0 |
![]() |
I dont know how you made your outlook work with 465 because it clearly does not work for me. And also the port or GMAIL is 587. SO naturally it will work for 587.
http://mail.google.com/support/bin/answer.py?answer=86375
Shoban Kumar
http://www.crankup.net :: My tech blog
http://www.codegeeks.net :: my code blog
![]() |
0 |
![]() |
Why would you be using port 465? Google REQUIRES you to use port 587 with a secure connection. That's google's setting and is not up to you, .net, outlook or whatever else. Google will only accept SMTP requests via port 587 over a secure connection.
![]() |
0 |
![]() |
From http://mail.google.com/support/bin/answer.py?hl=en&answer=13287 I get the information I can user either port 465 or port 587 !
When I use the code above to send email with the port 465, I don't get any error information, it's seems the email disappear :(
SuperCool Multiple ZIP - A utility to unzip multiple files and work with multiple zip files
SuperCool Random Number Generator
![]() |
0 |
![]() |
To shobankr ,
I do can send email with OutLook Express 6 with either port 465 or port 587
SuperCool Multiple ZIP - A utility to unzip multiple files and work with multiple zip files
SuperCool Random Number Generator
![]() |
0 |
![]() |