ASP.NET发送电子邮件到一个gmail账户。2个独立的电子邮件。
创始人
2024-09-18 21:01:18
0

下面是使用ASP.NET发送电子邮件到Gmail账户的代码示例:

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

namespace EmailExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 发送方的电子邮件和密码
            string senderEmail = "your_email@gmail.com";
            string senderPassword = "your_password";

            // 第一个电子邮件的收件人和内容
            string recipientEmail1 = "recipient1@gmail.com";
            string subject1 = "Hello from ASP.NET";
            string body1 = "This is the first email sent using ASP.NET.";

            // 第二个电子邮件的收件人和内容
            string recipientEmail2 = "recipient2@gmail.com";
            string subject2 = "Hello again";
            string body2 = "This is the second email sent using ASP.NET.";

            // 创建SMTP客户端
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(senderEmail, senderPassword);

            try
            {
                // 发送第一个电子邮件
                MailMessage message1 = new MailMessage(senderEmail, recipientEmail1, subject1, body1);
                client.Send(message1);
                Console.WriteLine("First email sent successfully.");

                // 发送第二个电子邮件
                MailMessage message2 = new MailMessage(senderEmail, recipientEmail2, subject2, body2);
                client.Send(message2);
                Console.WriteLine("Second email sent successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error sending email: " + ex.Message);
            }
        }
    }
}

请确保将your_email@gmail.comyour_password替换为您自己的Gmail帐户电子邮件和密码。请注意,这段代码使用了Gmail的SMTP服务器,并且要求您的Gmail帐户启用了SMTP访问权限。

此代码将创建一个SMTP客户端并发送两个独立的电子邮件到不同的Gmail收件人。第一个电子邮件的主题是"Hello from ASP.NET",内容是"This is the first email sent using ASP.NET."。第二个电子邮件的主题是"Hello again",内容是"This is the second email sent using ASP.NET."。

如果发送成功,控制台将显示"First email sent successfully."和"Second email sent successfully."。如果发送过程中出现错误,控制台将显示错误消息。

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...