本文将分享在C#语言的Winform应用中,实现邮件自动发送的代码及案例。邮件自动发送是一项常见的功能需求,我们可以通过在Winform界面中添加相应的控件和代码,来实现自动发送邮件的功能。下面是该功能的代码示例:
'''
using System;
using System.Net;
using System.Net.Mail;
namespace CSharpWinformMail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
try
{
string senderEmail = textBoxSenderEmail.Text;
string senderPassword = textBoxSenderPassword.Text;
string receiverEmail = textBoxReceiverEmail.Text;
string subject = textBoxSubject.Text;
string body = textBoxBody.Text;
SmtpClient smtpClient = new SmtpClient("smtp.server.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(senderEmail, senderPassword);
smtpClient.EnableSsl = true;
MailMessage mailMessage = new MailMessage(senderEmail, receiverEmail, subject, body);
smtpClient.Send(mailMessage);
MessageBox.Show("邮件发送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
'''
以上代码可以在C#的Winform项目中使用,传入发件人邮箱、发件人密码、收件人邮箱、邮件主题和邮件内容,然后通过SMTP协议发送邮件。大家可以根据自己的需求进行相应的修改和调整,实现个性化的邮件自动发送功能。
暂无评论