Created
February 17, 2019 19:46
-
-
Save mizuneko/aebc45e734ca33f2e22387b01f0aaf70 to your computer and use it in GitHub Desktop.
[MailKitでメール送受信] #MailKit #Newtonsoft.Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"EmailConfiguration": { | |
"SmtpServer": "smtp.gmail.com", | |
"SmtpPort": 587, | |
"SmtpUsername": "yourmailaddress@gmail.com", | |
"SmtpPassword": "applicationpassword", | |
"PopServer": "pop.gmail.com", | |
"PopPort": 995, | |
"PopUsername": "yourmailaddress@gmail.com", | |
"PopPassword": "yourpassword" | |
}, | |
"EmailMessage": { | |
"FromAddress": [ | |
{ | |
"Name": "", | |
"Address": "sampleFrom@example.com" | |
} | |
], | |
"ToAddress": [ | |
{ | |
"Name": "", | |
"Address": "sampleTo@example.com" | |
} | |
], | |
"Subject": "件名", | |
"Content": "メール本文\\nいかがでしょうか。" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EmailNet | |
{ | |
public class Config | |
{ | |
public EmailConfiguration EmailConfiguration{ get; set; } | |
public EmailMessage EmailMessage{ get; set; } | |
public EmailAddress EmailAddress { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EmailNet | |
{ | |
public class EmailAddress | |
{ | |
public string Name { get; set; } | |
public string Address{ get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EmailNet | |
{ | |
public class EmailConfiguration : IEmailConfiguration | |
{ | |
public string SmtpServer { get; set; } | |
public int SmtpPort { get; set; } | |
public string SmtpUsername { get; set; } | |
public string SmtpPassword { get; set; } | |
public string PopServer { get; set; } | |
public int PopPort { get; set; } | |
public string PopUsername { get; set; } | |
public string PopPassword { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace EmailNet | |
{ | |
public class EmailMessage | |
{ | |
public EmailMessage() | |
{ | |
ToAddress = new List<EmailAddress>(); | |
FromAddress = new List<EmailAddress>(); | |
} | |
public List<EmailAddress> ToAddress { get; set; } | |
public List<EmailAddress> FromAddress { get; set; } | |
public string Subject { get; set; } | |
public string Content { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using MailKit.Net.Pop3; | |
using MailKit.Net.Smtp; | |
using MailKit.Security; | |
using MimeKit; | |
using MimeKit.Text; | |
namespace EmailNet | |
{ | |
public class EmailService : IEmailService | |
{ | |
private readonly IEmailConfiguration _emailConfiguration; | |
public EmailService(IEmailConfiguration emailConfiguration) | |
{ | |
_emailConfiguration = emailConfiguration; | |
} | |
public List<EmailMessage> ReceiveEmail(int maxCount = 10) | |
{ | |
using (var emailClient = new Pop3Client()) | |
{ | |
emailClient.Connect(_emailConfiguration.PopServer, _emailConfiguration.PopPort, SecureSocketOptions.SslOnConnect); | |
emailClient.Authenticate(_emailConfiguration.PopUsername, _emailConfiguration.PopPassword); | |
List<EmailMessage> emails = new List<EmailMessage>(); | |
for (int i = 0; i < emailClient.Count && i < maxCount; i++) | |
{ | |
var message = emailClient.GetMessage(i); | |
var emailMessage = new EmailMessage | |
{ | |
Content = !string.IsNullOrEmpty(message.HtmlBody) | |
? message.HtmlBody : message.TextBody, | |
Subject = message.Subject | |
}; | |
emailMessage.ToAddress.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name })); | |
emailMessage.FromAddress.AddRange(message.From.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name })); | |
} | |
emailClient.Disconnect(true); | |
return emails; | |
} | |
} | |
public void Send(EmailMessage emailMessage) | |
{ | |
var message = new MimeMessage(); | |
message.To.AddRange(emailMessage.ToAddress.Select(x => new MailboxAddress(x.Name, x.Address))); | |
message.From.AddRange(emailMessage.FromAddress.Select(x => new MailboxAddress(x.Name, x.Address))); | |
message.Subject = emailMessage.Subject; | |
message.Body = new TextPart(TextFormat.Plain) | |
{ | |
Text = emailMessage.Content | |
}; | |
using (var emailClient = new SmtpClient()) | |
{ | |
emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, SecureSocketOptions.Auto); | |
emailClient.Authenticate(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword); | |
emailClient.Send(message); | |
emailClient.Disconnect(true); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EmailNet | |
{ | |
public interface IEmailConfiguration | |
{ | |
string SmtpServer { get; } | |
int SmtpPort { get; } | |
string SmtpUsername { get; set; } | |
string SmtpPassword { get; set; } | |
string PopServer { get; } | |
int PopPort { get; } | |
string PopUsername { get; } | |
string PopPassword { get; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace EmailNet | |
{ | |
public interface IEmailService | |
{ | |
void Send(EmailMessage emailMessage); | |
List<EmailMessage> ReceiveEmail(int maxCount = 10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Newtonsoft.Json; | |
namespace EmailNet | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = JsonConvert.DeserializeObject<Config>(LoadSettings()); | |
var emailService = new EmailService(config.EmailConfiguration); | |
if (args[0] == "receive") | |
{ | |
var receiveMessages = emailService.ReceiveEmail(10); | |
receiveMessages.ForEach(x => | |
{ | |
Console.WriteLine($"From:{x.FromAddress}"); | |
Console.WriteLine($"To:{x.ToAddress}"); | |
Console.WriteLine($"Subject:{x.Subject}"); | |
Console.WriteLine($"Content:{x.Content}"); | |
}); | |
} | |
if (args[0] == "send") | |
{ | |
var sendMessage = config.EmailMessage; | |
emailService.Send(sendMessage); | |
} | |
} | |
private static string LoadSettings() | |
{ | |
var dirName = Directory.GetCurrentDirectory(); | |
var fileName = @"appSettings.json"; | |
return File.ReadAllText(Path.Combine(dirName, fileName)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment