如何从.net发送邮件?

时间:2011-04-12 11:57:41

标签: email

我写了下面的方法我得到了错误因为指定的字符串不是电子邮件地址所需的格式。请帮助我

SendMail("xyz@gmail.com","hi","heloo");

public bool SendMail(string toMailAddress, string mailSubject, string mailMessage)
    {

         string smtphost ="smtp.gmail.com";
            int smtpport = 100;
            string smtpuser ="xyz";
            string smtppwd = "xyz";
            SmtpClient client = null;



            string MessageBody = string.Empty;
            try
            {

                message = new MailMessage();
                message.From = new MailAddress(smtpuser);
                message.To.Add(toMailAddress);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = mailSubject;
                message.Body = mailMessage.ToString();



                message.IsBodyHtml = true;

                client = new SmtpClient();
                client.Host = smtphost;

                client.Port = smtpport;
                client.Credentials = new System.Net.NetworkCredential(smtpuser, smtppwd);
                client.Send(message);
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
            return true;
        }

4 个答案:

答案 0 :(得分:0)

您的“来自”用户必须采用有效电子邮件地址的形式。

message.From = new MailAddress(smtpuser);

此外,您还需要使用.To属性的MailAddress构造函数。

答案 1 :(得分:0)

尝试,端口为25,IsSSLEnabled为true,因为gmail服务器已启用SSL

答案 2 :(得分:0)

确保toMailAddress和smtpuser是有效的电子邮件地址。 尝试,使用smtpport = 587;由Gmail提供的外发邮件(SMTP)。 希望这会使它工作正常。请列出您遇到的错误。

答案 3 :(得分:0)

此代码可以使用。我所做的是

  1. 正确的smtphost
  2. 正确的smtpport - 587
  3. 启用SSL
  4. 在设置凭据
  5. 之前将UseDefaultCredentials设置为false
  6. 设置DeliveryMethod
  7.    
     public static bool SendMail(string toMailAddress, string mailSubject, string   mailMessage)
        {            
            string smtphost = "smtp.gmail.com";
            int smtpport = 587;
            string smtpuser = "youremail@gmail.com";
            string smtppwd = "password";
    
            SmtpClient client = null;
    
    
    
            string MessageBody = string.Empty;
            try
            {
    
                var message = new MailMessage();
                message.From = new MailAddress(smtpuser);                
                message.To.Add(toMailAddress);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = mailSubject;
                message.Body = mailMessage.ToString();
                message.IsBodyHtml = true;
    
                client = new SmtpClient();
                client.Host = smtphost;                
                client.EnableSsl = true;
                client.Port = smtpport;
                client.UseDefaultCredentials = false;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = new System.Net.NetworkCredential(smtpuser, smtppwd);
    
                client.Send(message);
            }
            catch (Exception ex)
            {
                string x = ex.InnerException.Message;
                Console.WriteLine(x);
            }
            return true;
    
        }