C# WPF自动发送Thunderbird邮件带附件图片
以下是一个基本的 C# WPF 代码示例,用于自动发送 Thunderbird 邮件带附件图片:
using System;
using System.Net.Mail;
using System.Net.Mime;
using System.Windows;
using System.IO;
namespace ThunderbirdMailSender
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
try
{
// 创建邮件对象
MailMessage message = new MailMessage();
// 设置发件人地址
message.From = new MailAddress(txtFrom.Text);
// 设置收件人地址
message.To.Add(new MailAddress(txtTo.Text));
// 设置主题和正文
message.Subject = txtSubject.Text;
message.Body = txtBody.Text;
// 添加附件
if (txtAttachment.Text != "")
{
Attachment attachment = new Attachment(txtAttachment.Text, MediaTypeNames.Image.Jpeg);
message.Attachments.Add(attachment);
}
// 创建 SMTP 客户端对象
SmtpClient client = new SmtpClient("smtp.live.com", 587);
// 设置 SMTP 客户端的凭据
client.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtPassword.Password);
// 启用 SSL
client.EnableSsl = true;
// 发送邮件
client.Send(message);
MessageBox.Show("邮件发送成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show("邮件发送失败!" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void btnAttachment_Click(object sender, RoutedEventArgs e)
{
// 打开文件对话框,选择附件
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".jpg";
dlg.Filter = "JPEG 图片 (*.jpg)|*.jpg";
Nullable<bool> result = dlg.ShowDialog();
// 如果选择了文件,将其路径显示在文本框中
if (result == true)
{
txtAttachment.Text = dlg.FileName;
}
}
}
}
在 XAML 中,创建了一个包含文本框、按钮和标签的基本窗口界面,以及一个单击发送按钮时触发的事件处理程序。用户可以输入发件人、收件人、主题、正文和附件路径,然后单击发送按钮发送邮件。
<Window x:Class="ThunderbirdMailSender.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Thunderbird Mail Sender" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="From:"/>
<TextBox Grid.Row="0" Grid.Column="1" x:Name="txtFrom" Margin="5"/>
<Label Grid.Row="1" Grid.Column="0" Content="To:"/>
<TextBox Grid.Row="1" Grid.Column="1" x:Name="txtTo" Margin="5"/>
<Label Grid.Row="2" Grid.Column="0" Content="Subject:"/>
<TextBox Grid.Row="2" Grid.Column="1" x:Name="txtSubject" Margin="5"/>
<Label Grid.Row="3" Grid.Column="0" Content="Body:"/>
<TextBox Grid.Row="3" Grid.Column="1" x:Name="txtBody" Margin="5" Height="100"/>
<Label Grid.Row="4" Grid.Column="0" Content="Attachment:"/>
<TextBox Grid.Row="4" Grid.Column="1" x:Name="txtAttachment" Margin="5"/>
<Button Grid.Row="4" Grid.Column="2" x:Name="btnAttachment" Content="Browse" Click="btnAttachment_Click"/>
<Label Grid.Row="5" Grid.Column="0" Content="Username:"/>
<TextBox Grid.Row="5" Grid.Column="1" x:Name="txtUsername" Margin="5"/>
<Label Grid.Row="6" Grid.Column="0" Content="Password:"/>
<PasswordBox Grid.Row="6" Grid.Column="1" x:Name="txtPassword" Margin="5"/>
<Button Grid.Row="7" Grid.Column="1" x:Name="btnSend" Content="Send" Margin="5" Click="btnSend_Click"/>
</Grid>
</Window>
``
原文地址: https://www.cveoy.top/t/topic/hhEI 著作权归作者所有。请勿转载和采集!