如何使用 SmtpClient 将图像添加为附件

本文关键字:添加 图像 何使用 SmtpClient | 更新日期: 2023-09-27 18:31:49

我正在制作一个电子邮件客户端wpf应用程序。它工作正常,除了我不知道如何向其添加图像。我还想将我撰写的电子邮件保存为 PC 上的 pdf 或 docx 文件。

我搜索了很多,但找不到合适的答案。请帮忙

我的电子邮件方法是这样的:

 private void sendEmailB_Click(object sender, RoutedEventArgs e)
    {   SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.Credentials = new NetworkCredential(gmailID.Text, gmailPass.Password);
        client.EnableSsl = true;


        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(gmailID.Text);
        msg.To.Add(new MailAddress(sendEmailID.Text));
        msg.Subject = "handmail message";
        msg.Body = contentMain.Text;
        try
        {
            client.Send(msg);
            MessageBox.Show("Message sent successfully");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to send mail due to some reasons" + ex.Message);
        }
    }

如何使用 SmtpClient 将图像添加为附件

要添加图像附件,您可以简单地尝试一下

msg.Attachments.Add(new Attachment(imagePath));

其中 imagePath 包含要上传的图像的位置(路径)。