发送电子邮件的Windows窗体

本文关键字:Windows 窗体 电子邮件 | 更新日期: 2023-09-27 18:03:53

我正试图建立一个windows窗体3 textbox像Sender, Object, Body

要使用。net和以下版本的SmtpClient特性,允许用户使用我的windows窗体发送电子邮件。

我真的弄不清楚,请原谅我,因为我刚刚接触c#,我还在学习。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace ArcadiaPatcher
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential = new NetworkCredential("login", "pass");
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(Text1);
        smtpClient.Host = "mail.domain2.com";
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        message.From = fromAddress;
        message.Subject = Text2;
        //Set IsBodyHtml to true means you can send HTML email.
        message.IsBodyHtml = false;
        message.Body = Text3;
        message.To.Add("recipient");
        try
        {
            smtpClient.Send(message);
        }
        finally
        {
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    }


}

}

发送电子邮件的Windows窗体

下面是一个使用gmail smtp的工作示例:

    public static MailAddress from = new MailAddress("myAddress@gmail.com","Ramy Mohamed");
    string password = "balabala";
    var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(from.Address, password)
        };
       using (var message = new MailMessage(from, SendToAddress.Text)
                {
                    Subject = MySubjectText.Text,
                    Body = MyContentText.Text
                })
                {
                    smtp.Send(message);                        
                }