--- /dev/null
+using MimeKit;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MailDivSender
+{
+ /// <summary>
+ /// メール送信基底
+ /// </summary>
+ public interface BaseSendMail
+ {
+ /// <summary>
+ /// 送信
+ /// </summary>
+ /// <param name="callback"></param>
+ /// <param name="countcallback"></param>
+ /// <param name="addresspack"></param>
+ /// <returns></returns>
+ (bool result, Exception exception) Send(SendMailProgressCallback callback, SendMailProgressCountCallback countcallback, MailInfoPack addresspack);
+ }
+
+ /// <summary>
+ /// 送信進捗メッセージ
+ /// </summary>
+ /// <param name="message"></param>
+ public delegate void SendMailProgressCallback( string message );
+
+ /// <summary>
+ /// 送信進捗カウント
+ /// </summary>
+ /// <param name="iCount"></param>
+ public delegate void SendMailProgressCountCallback(int iCount);
+
+ /// <summary>
+ /// 送信アドレス情報
+ /// </summary>
+ public class SendInfoPack
+ {
+ /// <summary>
+ /// 種類
+ /// </summary>
+ public SelectedAddressControl.CCKindType Kind;
+
+ /// <summary>
+ /// アドレス
+ /// </summary>
+ public MailboxAddress Address;
+ }
+
+ /// <summary>
+ /// 送信メール情報
+ /// </summary>
+ public class MailInfoPack
+ {
+ /// <summary>
+ /// タイトル
+ /// </summary>
+ public string Subject;
+
+ /// <summary>
+ /// 内容
+ /// </summary>
+ public string Message;
+
+ /// <summary>
+ /// 差出人
+ /// </summary>
+ public MailboxAddress FromAddress;
+
+ /// <summary>
+ /// 送信先
+ /// </summary>
+ public MailboxAddress ToAddress;
+
+ /// <summary>
+ /// 送信先リスト
+ /// </summary>
+ public List<SendInfoPack> SendAddressList = new List<SendInfoPack>();
+
+ /// <summary>
+ /// 添付ファイルリスト
+ /// </summary>
+ public List<string> AttachList = new List<string>();
+ }
+}
<Compile Include="AddressControl.Designer.cs">
<DependentUpon>AddressControl.cs</DependentUpon>
</Compile>
+ <Compile Include="BaseSendMail.cs" />
<Compile Include="SenderForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ServerSettingDlg.Designer.cs">
<DependentUpon>ServerSettingDlg.cs</DependentUpon>
</Compile>
+ <Compile Include="SMTPSendMail.cs" />
<EmbeddedResource Include="AddressControl.resx">
<DependentUpon>AddressControl.cs</DependentUpon>
</EmbeddedResource>
--- /dev/null
+using MailKit;
+using MailKit.Net.Imap;
+using MailKit.Net.Smtp;
+using MimeKit;
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MailDivSender
+{
+ /// <summary>
+ /// SMTPメール送信
+ /// </summary>
+ public class SMTPSendMail : BaseSendMail
+ {
+ /// <summary>
+ /// コンストラクタ
+ /// </summary>
+ public SMTPSendMail()
+ {
+ }
+
+ /// <summary>
+ /// 送信
+ /// </summary>
+ /// <param name="callback"></param>
+ /// <param name="countcallback"></param>
+ /// <param name="addresspack"></param>
+ /// <returns></returns>
+ public (bool result, Exception exception) Send(SendMailProgressCallback callback, SendMailProgressCountCallback countcallback, MailInfoPack addresspack )
+ {
+ int iCount = 0;
+ int iAllCount = addresspack.AttachList.Count;
+
+ var Imap4Server = Properties.Settings.Default.Imap4Server;
+ var Imap4Enable = Properties.Settings.Default.Imap4Enable;
+ var ImapPort = Properties.Settings.Default.Imap4Port;
+ var Imap4Sentfolder = Properties.Settings.Default.Imap4Sentfolder;
+ var SMTPServer = Properties.Settings.Default.SMTPServer;
+ var SMTPPort = Properties.Settings.Default.SMTPPort;
+ var User = Properties.Settings.Default.User;
+ var Password = Properties.Settings.Default.Pass;
+
+ Exception exception = null;
+
+ try
+ {
+ using (var client = new SmtpClient())
+ {
+ client.Connect(SMTPServer, SMTPPort);
+ client.Authenticate(User, Password);
+
+ IMailFolder sentfolder = null;
+
+ using (var imapclient = new ImapClient())
+ {
+ if (Imap4Enable == true)
+ {
+ imapclient.Authenticated += (obj, args) =>
+ {
+ var acc = imapclient.Inbox.Open(FolderAccess.ReadWrite);
+ sentfolder = imapclient.Inbox.GetSubfolder(Imap4Sentfolder);
+ };
+
+ imapclient.Connect(Imap4Server, ImapPort, MailKit.Security.SecureSocketOptions.Auto);
+ imapclient.Authenticate(User, Password);
+ }
+
+ foreach (var filepath in addresspack.AttachList)
+ {
+ callback($"送信中 {filepath}");
+
+ var message = new MimeMessage();
+
+ var jis = Encoding.GetEncoding("iso-2022-jp");
+
+ //差出人
+ message.From.Add(addresspack.FromAddress);
+ //宛先
+ message.To.Add(addresspack.ToAddress);
+ //CC
+ foreach (var sendinfopack in addresspack.SendAddressList)
+ {
+
+ if (sendinfopack.Kind == SelectedAddressControl.CCKindType.CC)
+ message.Cc.Add(sendinfopack.Address);
+ if (sendinfopack.Kind == SelectedAddressControl.CCKindType.BCC)
+ message.Bcc.Add(sendinfopack.Address);
+ }
+ //件名
+ message.Headers.Replace(HeaderId.Subject, jis, $"{addresspack.Subject} ({iCount + 1}/{iAllCount})");
+ //本文
+ var text = new TextPart(MimeKit.Text.TextFormat.Plain);
+ text.SetText(jis, addresspack.Message);
+
+ var multipart = new MimeKit.Multipart("mixed");
+ multipart.Add(text);
+
+ //添付
+ var mimeType = MimeKit.MimeTypes.GetMimeType(filepath);
+ var attachment = new MimePart(mimeType)
+ {
+
+ Content = new MimeContent(File.OpenRead(filepath)),
+ ContentDisposition = new ContentDisposition(),
+ ContentTransferEncoding = ContentEncoding.Base64,
+ FileName = Path.GetFileName(filepath)
+ };
+ multipart.Add(attachment);
+ message.Body = multipart;
+
+ client.Send(message);
+
+ callback( $"送信済追加中 {filepath}");
+
+ if (Imap4Enable == true)
+ {
+ //送信済
+ sentfolder.Append(message);
+ }
+
+ callback( $"完了 {filepath}");
+
+ iCount++;
+
+ countcallback( iCount);
+ }
+ }
+
+ client.Disconnect(true);
+ }
+
+ return (result:true, exception:null);
+ }
+ catch (Exception ex)
+ {
+ exception = ex;
+ }
+
+ return (result:false, exception:exception);
+ }
+ }
+}
Enabled = false;
- Exception exception = null;
-
- var bResult = await Task.Run<bool>(() =>
+ //送信情報構築
+ var mailinfo = new MailInfoPack()
{
- var Imap4Server = Properties.Settings.Default.Imap4Server;
- var Imap4Enable = Properties.Settings.Default.Imap4Enable;
- var ImapPort = Properties.Settings.Default.Imap4Port;
- var Imap4Sentfolder = Properties.Settings.Default.Imap4Sentfolder;
- var SMTPServer = Properties.Settings.Default.SMTPServer;
- var SMTPPort = Properties.Settings.Default.SMTPPort;
- var User = Properties.Settings.Default.User;
- var Password = Properties.Settings.Default.Pass;
-
- try
- {
- using (var client = new SmtpClient())
- {
- client.Connect(SMTPServer, SMTPPort);
- client.Authenticate(User, Password);
-
- IMailFolder sentfolder = null;
-
- using (var imapclient = new ImapClient())
- {
- if (Imap4Enable == true)
- {
- imapclient.Authenticated += (obj, args) =>
- {
- var acc = imapclient.Inbox.Open(FolderAccess.ReadWrite);
- sentfolder = imapclient.Inbox.GetSubfolder(Imap4Sentfolder);
- };
-
- imapclient.Connect(Imap4Server, ImapPort, MailKit.Security.SecureSocketOptions.Auto);
- imapclient.Authenticate(User, Password);
- }
-
- foreach (var filepath in filelist)
- {
- Invoke((Action)(() => { StatusLbl.Text = $"送信中 {filepath}"; }));
-
- var message = new MimeMessage();
-
- var jis = Encoding.GetEncoding("iso-2022-jp");
-
- //差出人
- message.From.Add(SenderCnt.Get());
- //宛先
- message.To.Add(ToCtl.Get());
- //CC
- foreach (var ctl in CCAddressPanel.Controls)
- {
- var addctl = ctl as SelectedAddressControl;
-
- if (addctl.Kind == SelectedAddressControl.CCKindType.CC)
- message.Cc.Add(addctl.Get());
- if (addctl.Kind == SelectedAddressControl.CCKindType.BCC)
- message.Bcc.Add(addctl.Get());
- }
- //件名
- message.Headers.Replace(HeaderId.Subject, jis, $"{SubjectTxt.Text} ({iCount + 1}/{iAllCount})");
- //本文
- var text = new TextPart(MimeKit.Text.TextFormat.Plain);
- text.SetText(jis, MessageTxt.Text);
-
- var multipart = new MimeKit.Multipart("mixed");
- multipart.Add(text);
-
- //添付
- var mimeType = MimeKit.MimeTypes.GetMimeType(filepath);
- var attachment = new MimePart(mimeType)
- {
-
- Content = new MimeContent(File.OpenRead(filepath)),
- ContentDisposition = new ContentDisposition(),
- ContentTransferEncoding = ContentEncoding.Base64,
- FileName = Path.GetFileName(filepath)
- };
- multipart.Add(attachment);
- message.Body = multipart;
-
- client.Send(message);
-
- Invoke((Action)(() => { StatusLbl.Text = $"送信済追加中 {filepath}"; }));
-
- if (Imap4Enable == true)
- {
- //送信済
- sentfolder.Append(message);
- }
-
- Invoke((Action)(() => { StatusLbl.Text = $"完了 {filepath}"; }));
-
- iCount++;
-
- Invoke((Action)(() => { StatusPrg.Value = iCount; }));
- }
- }
-
- client.Disconnect(true);
- }
-
- return true;
+ Subject = SubjectTxt.Text,
+ Message = MessageTxt.Text,
+ FromAddress = SenderCnt.Get(),
+ ToAddress = ToCtl.Get(),
+ };
+ //CC
+ foreach (var ctl in CCAddressPanel.Controls)
+ {
+ var addctl = ctl as SelectedAddressControl;
- }
- catch (Exception ex)
+ mailinfo.SendAddressList.Add(new SendInfoPack()
{
- exception = ex;
- }
+ Kind = SelectedAddressControl.CCKindType.CC,
+ Address = addctl.Get(),
+ });
+ }
+ mailinfo.AttachList.AddRange(filelist);
- return false;
+ var bTResult = await Task.Run<(bool result, Exception exception)>(() =>
+ {
+ return new SMTPSendMail().Send(
+ (msg) => Invoke((Action)(() => { StatusLbl.Text = msg; })),
+ (iCnt) => Invoke((Action)(() => { StatusPrg.Value = iCount; })),
+ mailinfo
+ );
});
+ Exception exception = bTResult.exception;
+ var bResult = bTResult.result;
Enabled = true;