Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
We are working in Mailing system in asp.net,our client using Microsoft exchange server outlook.office365 . we are getting "System.Net.Mail.SmtpException: The operation has timed out error ". i google all sources but didn't work for me.Please help us. Thanks in advance.


btn_click(){
try{SmtpClient smtp = new SmtpClient("smtp.outlook.office365.com");
smtp.Host = "outlook.office365.com";
smtp.Port = System.Convert.ToInt32("443");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("userid", "mypwd");
smtp.Credentials = cred;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.TargetName = "STARTTLS/outlook.office365.com";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("frommailid", "username");
msg.To.Add(new MailAddress("xxx@xxx.com"));
msg.Subject = "Test";
msg.Body = "Test mail ";
smtp.Timeout = 60000;
smtp.Send(msg);
}catch(exception ex)
{
throw ex;
}
}
Posted
Updated 20-Oct-15 0:23am
v2

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
msg.From = new MailAddress("you@yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = ex.ToString();
}
 
Share this answer
 
Comments
kartheeknr 20-Oct-15 6:25am    
Please check my above code.587 port is not authenticating.
Thanks
kartheeknr 20-Oct-15 6:26am    
Above code is working for gmail and yahoo mail,but outlook.office365 exchange server is not working
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

//Read fromEmail from registery
string fromEmail = Registery.fromAddress;

//Read fromPassword from registery
string fromPassword = Registery.password;

//Read toAddress from registery
string toAddress = Registery.ToAddress;

//Read outgoing server name from registery
string smtpServer = Registery.smtpServer;

//Read port of outgoing server from registery
int port = Convert.ToInt32(Registery.port);

//Read whether enableSSL is true or false from registery
bool enableSSL = Convert.ToBoolean(Registery.enableSSL);

message.From = new MailAddress(fromEmail.ToString());
message.To.Add(toAddress.ToString());
message.Subject = ServiceName + " " + status.ToString();
message.Body = "Service Name :" + ServiceName + Environment.NewLine + "Status :" + status.ToString();
message.IsBodyHtml = true;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

///smtp server and port configured at registry
SmtpClient smtpClient = new SmtpClient(smtpServer, port);

///enable ssl is required for secure connection.It is must be true for gmail server and false for other servers.
smtpClient.EnableSsl = enableSSL;

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail,fromPassword);
smtpClient.Send(message);
}

catch (Exception ex)
{

Logger.Log("Error In Sending Mail. " + ex.Message + " , " + ex.InnerException, 3);
}
 
Share this answer
 
Comments
Gokulprasad05 20-Oct-15 6:31am    
smtp-mail.outlook.com Port no :465
smtp.gmail.com and 587 for gmail .........
 
Share this answer
 
Comments
kartheeknr 20-Oct-15 8:58am    
i want to Send Mails in asp.net using Exchange server(smtp:outlook.office365.com) ,not gmail.
gmail is working good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900