在C#中发送电子邮件选项 [英] Send Email Options In C#

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

问题描述



我有简单的C#脚本,通过Gmail SMTP发送电子邮件,效果非常好。

我有疑问:

1 - 如何在此代码中启用Htmal电子邮件格式?

2-如何在此代码中添加回复选项?

例如代码发送自:a @ a。当收件人电子邮件想要回复时,它会自动切换到b@b.com。



Hi,
I have simple C# script that sends Email by Gmail SMTP and works very good.
I have questions:
1- How enable Htmal email format in this code?
2- How to add "reply to" option to this code?
for example code send from: a@a.com and when receiver email want reply it automatically switch to b@b.com .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Reflection;
using System.IO;
namespace SendEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string []ee = {"",""};
            string[] nn = {"",""};
            
                for (int i = 0; i < ee.Length; i++)
                {
                    Email(ee[i], nn[i]);
                    Console.WriteLine("Step " + i + " finished" + "   Email Sent To: " + nn[i] + "(" + ee[i] + ")");
                    
}
            
            Console.ReadLine();
        }
        static void Email(string ename,string name)
        {
            var fromAddress = new MailAddress("", "");
            var toAddress = new MailAddress(ename, name);
            const string fromPassword = "";
 const string subject = "This is from test";
            const string body = "This is test";
    var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                 
                Timeout = 20000
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body,
               

            })
            {
               
                smtp.Send(message);
            }
    
        }

        
    }
}





问候,



Regards,

推荐答案

MSDN是你的朋友。



如果你设置 IsBodyHtml [ ^ ] poperty,你只需创建一个html文本身体。



以下是如何为邮件制作多个视图(纯文本和html): http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx [ ^ ]



还有我的 ReplyToList [ ^ ]你可以用它来设置返回地址。



你怎么没找到这些?他们都是同一个 MailMessage 的所有成员[ ^ ] class ...
MSDN is your friend.

If you set IsBodyHtml[^] poperty, you simply have to create a html text for the body.

Here is how you can make multiple view for the mail (both plain text and html): http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx[^]

And there is the ReplyToList[^] that you can use it to set the return address.

How is that you haven't find these? They are all the members of the same MailMessage[^] class...


我会假设您有目的地取出了电子邮件地址值以确保安全。



话虽如此,假设您将这些电子邮件地址放回代码中,以下代码应该适合您。



I will assume that you took out the email address values purposefully for security.

With that said, the following code should work for you assuming you put those email addresses back into your code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Reflection;
using System.IO;
namespace SendEmail
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] ee = { "", "" };
            string[] nn = { "", "" };

            for (int i = 0; i < ee.Length; i++)
            {
                Email(ee[i], nn[i]);
                Console.WriteLine("Step " + i + " finished" + "   Email Sent To: " + nn[i] + "(" + ee[i] + ")");

            }

            Console.ReadLine();
        }
        static void Email(string ename, string name)
        {
            var fromAddress = new MailAddress("", "");
            var toAddress = new MailAddress(ename, name);
            const string fromPassword = "";
            const string subject = "This is from test";
            const string body = "This is test";
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),

                Timeout = 20000
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body,
                IsBodyHtml = true, // ADDED: mailmessage body is html
                ReplyTo = new MailAddress("reply-to@example.com") // ADDED: reply to address addition 
            })
            {

                smtp.Send(message);
            }

        }


    }
}


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

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