0

そこで、Visual 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 WindowsFormsApplication9
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string Host = "smtp.live.com";
        Int16 Port = 587;
        bool SSL = true;
        string Username = "myemail@hotmail.com";
        string Password = "mypassword";

        // Mail options
        string To = "myemail@hotmail.com";
        string From = "email@hotmail.com";
        string Subject = "This is a test";
        string Body = "It works!";

        MailMessage mm = new MailMessage(From, To, Subject, Body);
        SmtpClient sc = new SmtpClient(Host, Port);
        NetworkCredential netCred = new NetworkCredential(Username, Password);
        sc.EnableSsl = SSL;
        sc.UseDefaultCredentials = false;
        sc.Credentials = netCred;

        MessageBox.Show("Test");
    }
}
}

*注意してください、これからエラーは発生しません。

4

2 に答える 2

2

実際にメールを送信することはありません。

これをコードに追加します-どこにあるかを理解できるはずです:

sc.Credentials = netCred;

try 
{
  sc.Send(message);
}  
catch (Exception ex) 
{
  MessageBox(ex.ToString());              
}              
MessageBox.Show("Test");
于 2012-04-14T02:40:53.083 に答える
-1

これらの名前空間を追加します...

using System.Net.Mail;
using System.Net;
using System.Configuration;

メールを送信するCode-Behindファイルに次のコード行を追加します。

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("yourEmailId", "destinationEmailId");
mail.Subject = ""; //Enter the text for the subject of the mail in quotes.
mail.Body = ""; //Enter the text for the body of mail within the quotes.
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.live.com");
NetworkCredential cred = new NetworkCredential("yourEmailId", "yourPassword");
client.EnableSsl = true;
client.Credentials = cred;
try
{
client.Send(mail);
}
catch (Exception)
{
}
于 2012-04-14T02:42:26.543 に答える