在C#中发送SMTP邮件时出现问题 [英] Problem in SMTP mail sending in c#

查看:125
本文介绍了在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.


这篇关于在C#中发送SMTP邮件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆