c# - 在C#3.5中发送电子邮件失败

标签 c# .net

发送电子邮件时,出现以下错误:


  设备尚未准备就绪,位于System.Net.Mail.SmtpClient.Send(MailMessage消息)。


代码是:

MailMessage mailMessage = new MailMessage(senderEmail, cleanRecipients)
{
    Subject = string.empty,
    Body = string.empty,
    IsBodyHtml = false
};
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);

最佳答案

此代码可能会有所帮助!

string from = me@gmail.com; //Replace this with your own correct Gmail Address

string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");

client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
       try
        {
            client.Send(mail);
        }
        catch (Exception ex) 
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty; 
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
   HttpContext.Current.Response.Write(errorMessage );
        } // end try 

关于c# - 在C#3.5中发送电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4468985/

相关文章:

c# - 在 .net 中针对来自 visual studio 测试客户端的不同机器对 WCF 进行单元和性能测试的最佳方法是什么

c# - 如何在 Visual Studio 2008 中重命名现有的解决方案和项目?

c# - 为动画目的简化对对象成员的引用的创建?

c# - 相对于另一个表单 vb/c# 中的按钮移动 winform

c# - ASP.NET Web api 上的文件上传端点损坏文件

c# - 如何判断一个值是否在 List<string> 中?

c# - 如何均衡两个列表中的记录数以在 C# 中进行计算

c# - WCF 服务中的 SQLite AccessViolationException

c# - 迭代器和枚举器的区别

c# - 无法从 Derived<Derived 2T> 转换为 BaseT<base 2T>