I try to send email from my application. Everything seems ok, mailmessage, credentials etc.
When I debug the code it doesnt fall in to catch exception and doesnt send 开发者_运维知识库email. Also there is no mail queue at the server.There is no error message.
msgObj.Subject = this.Subject;
msgObj.From = new MailAddress(this.From , this.Display_Name);//
msgObj.Body = this.Message;
msgObj.IsBodyHtml = true;
SmtpClient client = new SmtpClient(this.SMTP_Server,25);
client.Credentials = new System.Net.NetworkCredential(SMTP_User + "@doping.com.tr", SMTP_Password);
try
{
client.Send(msgObj);
return true;
}
catch (Exception ex)
{
ex.ToString();
return false;
}
What could be the problem?
Thanks.
The default settings of an SmtpClient
can be configured using the System.Net
configuration namespace, e.g:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="ben@contoso.com">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
See the <smtp> Element (Network Settings) topic for more information.
Check for port. Is port 25 you are using support. Also check for SSL. If still not works. Try below code.
/// <summary>
/// Transmit an email message
/// </summary>
/// <param name="from">Senders Name </param>
/// <param name="fromPerson">Sender Email Address</param>
/// <param name="body">The Email Message Body</param>
/// <returns>Status Message as String</returns>
public static void SendMail(string fromEmail, string fromName, string body)
{
try
{
using (MailMessage mail = new MailMessage())
{
mail.To.Add("abc@gmail.com");
mail.From = new MailAddress(fromEmail, fromName);
mail.Subject = "Report ";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = global::ProjectName.Properties.Settings.Default.Host;
smtp.Port = global::ProjectName.Properties.Settings.Default.Port;
if (smtp.Port == 587)
{
smtp.EnableSsl = true;
}
string userName = global::ProjectName.Properties.Settings.Default.UserName;
string password = global::ProjectName.Properties.Settings.Default.Password;
smtp.Credentials = new NetworkCredential(userName, password);
smtp.Send(mail);
}
}
catch (SmtpException smEx)
{
LogError(smEx.Message, smEx.StackTrace);
}
}
Here I am using Settings file of the project and retrieving values from there.
精彩评论