用C#发送电子邮件?

时间:2010-06-21 10:36:31

标签: c#

我正在尝试发送邮件,但此代码引发错误“发送失败”

MailMessage MailMesaji = new MailMessage();
MailMesaji.Subject = "subject";
MailMesaji.Body = "mail body";
//MailMesaji.BodyEncoding = Encoding.GetEncoding("Windows-1254"); // Turkish Character Encoding
MailAddress mdrom = new MailAddress("amit.pandey@verydindai.com");
MailMesaji.From = mdrom;
MailMesaji.To.Add(new MailAddress("govind@verydindai.com"));

System.Net.Mail.SmtpClient Smtp = new SmtpClient();
Smtp.Host = "mail.verydindai.com"; // for example gmail smtp server
Smtp.EnableSsl = true;
Smtp.Port = 465;

Smtp.Credentials = new System.Net.NetworkCredential("amit.pandey", "1234567");
Smtp.Send(MailMesaji);

请告诉我解决方案?如果你有其他解决方案告诉我?

4 个答案:

答案 0 :(得分:4)

gmail是否使用不同的smtp端口,而不是默认的25?

http://mail.google.com/support/bin/answer.py?hl=en&answer=13287

答案 1 :(得分:1)

 string to = "";
    string from = "";
    string from_pwd = "";
    MailMessage mM = new MailMessage();
    mM.From = new MailAddress(from);
    mM.To.Add(to);
    mM.Subject = subject;
    mM.Body = body;
    mM.IsBodyHtml = true;
    mM.Priority = MailPriority.High;
    SmtpClient sC = new SmtpClient("smtp.gmail.com");
    sC.Port = 587;
    sC.Credentials = new NetworkCredential(from, from_pwd );
    sC.EnableSsl = true;
    sC.Send(mM);

答案 2 :(得分:1)

您可能需要更改要连接的端口,我认为如果启用SSL,Google应该是465或587。

同时检查您是否有可能干扰的某些AntiVirus软件。 AV软件停止自定义软件发送电子邮件是很常见的。可能还有一些防火墙无法通过所需的端口。

答案 3 :(得分:1)

我写了一个控制台应用程序,请尝试使用此示例。 使用您的凭据要发送地址,来自地址,密码,正文

using System;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace TestingConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string to = "to@domain.com";
                string from = "from@gmail.com";
                string from_pwd = "mypassword";
                string subject = "Sample Mail testing";
                string body = "Wow this is testing body";
                MailMessage mM = new MailMessage();
                mM.From = new MailAddress(from);
                mM.To.Add(to);
                mM.Subject = subject;
                mM.Body = body;
                mM.IsBodyHtml = false;
                mM.Priority = MailPriority.High;
                SmtpClient sC = new SmtpClient("smtp.gmail.com");
                sC.Port = 587;
                sC.Credentials = new NetworkCredential(from, from_pwd);
                sC.EnableSsl = true;
                sC.Send(mM);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
            }
        }
    }
}