在C#中使用SMTP发送电子邮件 [英] Send Email using SMTP in C#

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

问题描述

using System.Net;
using System.Net.Mail;
OpenFileDialog dlg = new OpenFileDialog();
 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string filename = dlg.FileName;
 
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
 
                mail.From = new MailAddress("me@mydomain.com");
                mail.To.Add("u@urdomain.com");
                mail.Subject = filename;
                mail.Body = "Report";
                Attachment attachment = new Attachment(filename);
                mail.Attachments.Add(attachment);
 
                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);
            }









这些代码非常适用于但是,我只能发送纯文本作为电子邮件。如何发送带有格式功能的电子邮件,如Yahoo Mail中的粗体斜体颜色更改等。此外,我需要一个RichTextBox代码来包含所有这些按钮。



请帮助。





These codes are working perfectly for me however I can send only plain text as Email. How to send an Email with formatting features like bold italics color change etc as in Yahoo Mail. Also I need a RichTextBox code for includng all such buttons.

pls help.

推荐答案

检查这个out:



使用格式化文本编辑器和附件从ASP.NET发送电子邮件 [ ^ ]



希望它有所帮助:)
Check this out:

Sending Email from ASP.NET using Formatted Text Editor and Attachments[^]

hope it helps :)


基本上,您需要多部分电子邮件;一个或多个部分可以是HTML。或RTF,我不推荐。对于电子邮件,内容类型应为:内容类型:multipart / alternative multipart / mixed 。对于每个部分,内容类型应该是有效的MIME类型,例如 text / html



请请参阅:

http://en.wikipedia.org/wiki/MIME [ ^ ],

http://en.wikipedia.org/wiki/HTML_email [ ^ ]。



在.NET中发送电子邮件:

http://msdn.microsoft.com/en-us/library/ system.net.mail.mailmessage.aspx [ ^ ]。



对于更复杂的情况,其中一个部分可以引用另一个部分,例如HTML中引用的图像,请看我最近的回答:

使用图像发送HTML附件 [ ^ ]。



本文中的源代码还说明了如何使用C#编写多部分电子邮件。



请参阅此CodeProject文章:

使用或不使用附件在C#中发送电子邮件:通用例程。 [ ^ ]。



-SA
Basically, you need multi-part e-mail; and one or more parts could be HTML. Or RTF, which I would not recommend. For the e-mail, the content type should be: Content-Type: multipart/alternative or multipart/mixed. For each part, the content type should be a valid MIME type, such as text/html.

Please see:
http://en.wikipedia.org/wiki/MIME[^],
http://en.wikipedia.org/wiki/HTML_email[^].

Sending e-mail in .NET:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^].

For more complex cases where one part can reference another part, such as image referenced in HTML, please see my recent answer:
Sending HTML attachment with images[^].

The source code in this article also shows how to compose multipart e-mail using C#.

Please also see this CodeProject article:
Sending an Email in C# with or without attachments: generic routine.[^].

—SA


请参阅此处的示例



http://forums.asp.net/t/971802.aspx [ ^ ]
See the Example here

http://forums.asp.net/t/971802.aspx[^]


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

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