Skip to content

Instantly share code, notes, and snippets.

@mizuneko

mizuneko/Config.cs

Created Feb 17, 2019
Embed
What would you like to do?
[MailKitでメール送受信] #MailKit #Newtonsoft.Json
{
"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いかがでしょうか。"
}
}
namespace EmailNet
{
public class Config
{
public EmailConfiguration EmailConfiguration{ get; set; }
public EmailMessage EmailMessage{ get; set; }
public EmailAddress EmailAddress { get; set; }
}
}
namespace EmailNet
{
public class EmailAddress
{
public string Name { get; set; }
public string Address{ get; set; }
}
}
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; }
}
}
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; }
}
}
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);
}
}
}
}
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; }
}
}
using System.Collections.Generic;
namespace EmailNet
{
public interface IEmailService
{
void Send(EmailMessage emailMessage);
List<EmailMessage> ReceiveEmail(int maxCount = 10);
}
}
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