Error on Sending mail asp.net via console applications
I am have written a test console application for retrieving the data and sending mail from the message queue.My console application resides in the same solution of my main project.In my message queue lies the mail messages.When i run my console application there is a call to the mail sending method but the problem is mail is not getting sent.seenms that mailSettings that i have given in web.config file is not getting applied when i am sending mail via the console applications.Because when i am sending mail directly without the help of console application the message is getting send successfully.Also when i specify the mail setting options in the source code itself via smtp properties mail is getting send via console applications.But i want to send the mail via console application with the mail setting set on web.congig.
How can i overcome this problem.
Best regards.
My setting in web.config.
My code for sending mails
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body,List attachments) { // Instantiate a new instance of MailMessage MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
//string[] arrEmailTo = to.Split(',');
//foreach (string itemTo in arrEmailTo)
//{
// //mMailMessage.To.Add(new MailAddress(itemTo));
//}
mMailMessage.To.Add(to);
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMa开发者_高级运维ilMessage.CC.Add(new MailAddress(cc));
}
// Set the subject of the mail message
mMailMessage.Subject = subject;
//mMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
//int Idd=BitConverter.ToInt32(Encrypt((Id.Id).ToString()),0);
// byte[] IdHash = Encrypt((Id.Id).ToString());//generating hassh value
//int IdHash1 = BitConverter.ToInt32(IdHash, 0);
//passing the hash value by encoding it
// Set the body of the mail message
mMailMessage.Body = body;
//include the attachments if any
if(attachments!=null)
foreach (string file in attachments)
{
Attachment aa = new Attachment(UpPath+file);
mMailMessage.Attachments.Add(aa);
}
// Secify the format of the body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.High;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.EnableSsl = true;
//********* //With these options set in the source code mail is getting sent fine //******
//mSmtpClient.Host = "smtp.gmail.com";
//mSmtpClient.Port = 25;
// Send the mail message
//System.Net.NetworkCredential s = new System.Net.NetworkCredential("username@gmail.com", "password");
//mSmtpClient.Credentials = s;
//mSmtpClient.Port = 587;
mSmtpClient.Send(mMailMessage);
//********* //With these options set in the source code mail is getting sent fine //****** }
Thanks in Advance
Just for your testing, add an App.Config to the Console project with your mail settings.
精彩评论