发送电子邮件在C#中? [英] Sending E-mail in C#?

查看:138
本文介绍了发送电子邮件在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送邮件,但该代码引发错误发送失败

I am trying to send mail but this code raise error "Sending failed"

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);



PLZ告诉我的解决方案?如果你有另一种解决办法告诉我?

plz tell me solution? and if you have another solution tell me?

推荐答案

我写下一个控制台应用程序,请使用此示例尝试。
与您的凭据为解决,从地址,密码,正文

I am write down a console application please try with this sample. with your credentials To address,From Address,Password,Body text

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);
            }
        }
    }
}

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

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