Windows表单发送电子邮件

时间:2014-05-17 12:50:32

标签: c# .net email

我正在尝试使用3个TextBoxt构建一个Windows窗体,例如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)
    {

    }




}

}

1 个答案:

答案 0 :(得分:0)

以下是使用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);                        
                }