添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
4
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 3 years have passed since last update.

【C#を使ってOutlookにメール送信】メールを送信するコンソールアプリ作ってみた!

Last updated at Posted at 2021-03-15

【実行環境】
開発環境:Visual Studio2019 Community

Outlookへメール送信する際、いくつか躓いたのでその備忘録を作成します。

①まずOutlookのライブラリをCOM参照で取得します。
[プロジェクト]⇒[COM参照の追加]

②参照マネージャからOutlookのライブラリにチェックを入れて「OK」ボタンを押します。

③[コレ大事!] interop.Microsoft.Office.Outlookをクリックしてプロパティウィンドウを開きます。※赤枠の箇所に注目してください!
「ローカルにコピー」欄に「はい」を選択します。
「相互運用型の埋め込み」欄に「はい」を選択します。

あとは下記のコードをコピペするだけです!

【ソースコード】

Program.cs
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookMail
    class Program
        static void Main(string[] args)
            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;
            //メール本文の内容を記載する(例:"C#でOutlookを操作する")。
            mail.Body = "C#でOutlookを操作する";
            mail.Recipients.Add("[email protected]");
            mail.Recipients.ResolveAll();
            mail.Send();

コンソールアプリを実行!すると...
無事に送信メールを受信することが出来ました。

【お世話になった引用元】
.NET 5プロジェクトでのCOM参照の追加
URL: https://opcdiary.net/net-5%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%A7%E3%81%AEcom%E5%8F%82%E7%85%A7%E3%81%AE%E8%BF%BD%E5%8A%A0/

4
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
4
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?