
July 25th, 2011, 01:05 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 18
Time spent in forums: 2 h 57 m 16 sec
Reputation Power: 0
|
|
|
Custom mail sending error
Hi
I have C# code to send mail through gmail server. But when i want to use it in my server(win2003), it not work.
I had another server(win2003) and this code worked correctly on it, and it work correctly in my local computer(win7).
Do anyone know what is the problem?
public class EMail
{
static string EmailFrom = "mymail@gmail.com";
static string EmailDisplayName = "display name";
static string Password = "myPassword";
static string Host = "smtp.gmail.com";
//static int Port = 587;
static int Port = 465;
static public bool SendMail(string Subject, string Body, string EmailTo)
{
try
{
//create mail.
MailMessage message = new MailMessage();
message.From = new MailAddress(EmailDisplayName + "<" + EmailFrom + ">");
message.To.Add(new MailAddress(EmailTo));
message.Subject = Subject;
message.Body = "Stream, MediaTypeNames.Text.Html";
//Create alternate view.
string htmlBody = Body;
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
message.AlternateViews.Add(avHtml);
SmtpClient sc = new SmtpClient(Host);
sc.Port = Port;
sc.Credentials = new NetworkCredential(EmailFrom, Password);
sc.EnableSsl = true;
sc.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
Best Regards.
morteza
|