2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C# .NET】OutLookからファイルを添付してメールを送信

Last updated at Posted at 2019-07-05

似たような記事もあるとは思うが、自分用の備忘録に投稿。

概要

もはやタイトルの通り。
C#でOutLookを動かしてメールを指定したアドレスに送信する。
その際に任意のファイルを添付して送信。
プログラマ歴数か月のペーペーなのでコードが汚かったりおかしかったりしても堪忍な。
.NETframework 4.6.1どす

準備

[NuGetパッケージマネージャー] → [ソリューションのNuGetパッケージの管理]で
「Microsoft.Office.Interop.Outlook」をドン。
これだけ。

コード

SendMail.cs
using OutLook = Microsoft.Office.Interop.Outlook;

namespace Mail
{
    class SendMail
    {
        public void sendMail()
        {
            var ol = new Outlook.Application();
            Outlook.MailItem mail = ol.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mail.Subject = "メールタイトル";
            Outlook.AddressEntry currentUser = ol.Session.CurrentUser.AddressEntry;
            mail.Body = "メール本文";
            mail.Recipients.Add("送信先");
            mail.Attachment.Add("添付するファイルのパス");
            mail.Recipients.ResolveAll();
            mail.Send();
        }
    }
}

思ったよりも簡単でびっくり。
やったね。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?