在C#中发送SMTP邮件时出现问题
问题描述:
当出现异常然后成功发送邮件时,我正在使用以下代码,但是当没有异常时,则基数为64的char数组的长度无效.请回复我...
Hi,
I m using the below code when exception come then mail send successfully, but when no exception then invalid length for a base-64 char array. Pleas reply me...
public void UpdateData()
{
try
{
sendMail(UpdateData());//here message is success
}
Catch(Exception ex)
{
sendMail(ex.Message)
}
}
public void sendMail(string strExceptionMsg)
{
MailMessage objMailMessage = null;
SmtpClient SmtpMail = null;
System.IO.StreamWriter Log=null;
try
{
using ( Log Log = new System.IO.StreamWriter(apppath + "\\ImportLogMail.txt", true))
{
Log.WriteLine(DateTime.Now.ToString() + "Start Sending Email.......");
objMailMessage = new MailMessage();
objMailMessage.To.Add(MailTo);
objMailMessage.From = new MailAddress(MailFrom);
if (resultId == 0)
{
objMailMessage.IsBodyHtml = false;
objMailMessage.Subject = "Branch Code Update Status";
if (isValidTextFile)
{
objMailMessage.Body = "Hi" + Environment.NewLine + "Please find the attached text file for log information.";
}
else
{
objMailMessage.Body = "The file:" + fileName + " content of the file was not correct format";
}
Attachment attach = new Attachment(apppath + "\\ImportLog.txt");
objMailMessage.Attachments.Add(attach);
}
else
{
objMailMessage.IsBodyHtml = false;
objMailMessage.Subject = "Branch Code Update Status -Exception";
objMailMessage.Body = "Hi" + Environment.NewLine + "The following error occured while running the project." + Environment.NewLine + strExceptionMsg;
}
SmtpMail = new SmtpClient();
SmtpMail.Host = SMTPHost;
SmtpMail.Port = Convert.ToInt32(SMTPPort);
//SmtpMail.Timeout = 50000;
SmtpMail.EnableSsl = false;
SmtpMail.Credentials = new System.Net.NetworkCredential(SMTPUserId, SMTPPassword);
SmtpMail.UseDefaultCredentials = true;
SmtpMail.Send(objMailMessage);
Log.WriteLine(DateTime.Now.ToString() + "End Sending Email.......");
}
catch (SmtpException ex)
{
Log.WriteLine(ex.message); }
答
已答应从未答复的列表中删除-由OP解决.
Answered to remove from unanswered list - solved by OP.