从SMTP服务器发送邮件 [英] sending mail from SMTP server

查看:215
本文介绍了从SMTP服务器发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我的邮件功能无效,任何人都可以帮助我...

 MailMessage mail =  new  MailMessage(); 
SmtpClient SmtpServer = new SmtpClient( mail .abc.com);

mail.From = new MailAddress( abc@abc.com); // 您必须提供您的gmail地址,地址为
mail.To.Add( txtmail.Text);
mail.Subject = 注册;
mail.Body = Hello Mr / Mrs + txtuname.Text + 您的密码是 +密码;

SmtpServer.Port = 25 ;
SmtpServer.Credentials = new System.Net.NetworkCredential( gamil-username gmail-passowrd); // 您必须为您提供gamil用户名和密码
SmtpServer.EnableSsl = ;

SmtpServer.Send(mail);

解决方案

使用System.Net; 
使用System.Net.Mail;
使用System.Net.Mime;

...
尝试
{

SmtpClient mySmtpClient = new SmtpClient(my.smtp.exampleserver.net);

//使用basicAuthentication设置smtp-client
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential(username,password);
mySmtpClient.Credentials = basicAuthenticationInfo;

//从mailaddresses添加
来自= new MailAddress的邮件地址(test@example.com,TestFromName);
MailAddress to = new MailAddress(test2@example.com,TestToName);
MailMessage myMail = new System.Net.Mail.MailMessage(from,to);

//添加ReplyTo
MailAddress replyto = new MailAddress(reply@example.com);
myMail.ReplyTo = replyto;

//设置主题和编码
myMail.Subject =测试消息;
myMail.SubjectEncoding = System.Text.Encoding.UTF8;

//设置正文消息和编码
myMail.Body =< b>测试邮件< / b>< br>使用< b> HTML< / b>。 ;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text或html
myMail.IsBodyHtml = true;

mySmtpClient.Send(myMail);
}

catch(SmtpException ex)
{
抛出新的ApplicationException
(发生了SmtpException:+ ex.Message);
}
catch(exception ex)
{
throw ex;
}




在Codeproject网站上提问之前,请搜索codeproject。

登录后查看此内容

http://www.codeproject.com/search.aspx?q=sending+mail+from+smtp&x=10&y=5&sbo=qa&usfc=false

I'm having the following code but my mail functionality is not working can anybody please help me...

MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("mail.abc.com");

            mail.From = new MailAddress("abc@abc.com"); //you have to provide your gmail address as from address
            mail.To.Add(txtmail.Text);
            mail.Subject = "Registration";
            mail.Body = "Hello Mr/Mrs" + txtuname.Text + "your password is"+password;

            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("gamil-username", "gmail-passowrd"); //you have to provide you gamil username and password
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

解决方案

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("test@example.com", "TestFromName");
   MailAddress to = new MailAddress("test2@example.com", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyto = new MailAddress("reply@example.com");
   myMail.ReplyTo = replyto;

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;

   // set body-message and encoding
   myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
   myMail.BodyEncoding = System.Text.Encoding.UTF8;
   // text or html
   myMail.IsBodyHtml = true;

   mySmtpClient.Send(myMail);
}

catch (SmtpException ex)
{
  throw new ApplicationException
    ("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
   throw ex;
}


Hi,
Before asking question on Codeproject site ,please search on codeproject.
For this look at this after login
http://www.codeproject.com/search.aspx?q=sending+mail+from+smtp&x=10&y=5&sbo=qa&usfc=false


这篇关于从SMTP服务器发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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