コンテンツにスキップ

SendMailApp[送信テスト用][C#]

//Microsoft Visual Studio 2010 Projectファイルのダウンロードは最下部
//送信テスト用
//メール本文のエンコードを色々指定して送信
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;

namespace SendMailApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //参考URL
            //DOBON.NET
            //http://dobon.net/vb/dotnet/internet/smtpauth.html


            System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
            //SMTPサーバーを指定する
            sc.Host = "SMTPサーバー";

            //SMTP認証 ユーザー名とパスワードを設定する
            //SMTP認証をしない場合は、コメントアウト
            sc.Credentials = new System.Net.NetworkCredential("SMTPユーザー名", "SMTPパスワード");

            //SSL
            sc.EnableSsl = false;

            //Timeout
            //sc.Timeout = 100000;

            //SMTPポート番号
            sc.Port = 587;



            //MailMessageの作成
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            msg.From = new MailAddress(From_textBox.Text, "");

            msg.To.Add(new MailAddress(To_textBox.Text, ""));

            msg.Subject = Subject_textBox.Text;


            //本文設定
            string sBody = Body_textBox.Text;


            /*
            Content-Type
            charset
            Content-Transfer-Encoding
            を設定
            */

            //Content-Type: text/plain; charset=iso-2022-jp
            string ct = System.Net.Mime.MediaTypeNames.Text.Plain;
            AlternateView altView = AlternateView.CreateAlternateViewFromString(sBody, Encoding.GetEncoding("iso-2022-jp"), ct);

            //Content-Transfer-Encoding: Base64 | quoted-printable | SevenBit
            altView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;

            msg.AlternateViews.Add(altView);


            //メッセージを送信する
            sc.Send(msg);

            //後始末
            msg.Dispose();
        }


    }
}
ダウンロード 詳細 バージョン
SendMailApp.zip v.1
Back to top