Skip to content

Instantly share code, notes, and snippets.

@ykon
Last active November 19, 2017 15:11
Show Gist options
  • Save ykon/b563f177c30a318cf95db354db076f9a to your computer and use it in GitHub Desktop.
Save ykon/b563f177c30a318cf95db354db076f9a to your computer and use it in GitHub Desktop.
sendmail test
/*
* Copyright (c) 2017 Yuki Ono
* Licensed under the MIT License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace sendmail_test {
class Program {
static string get_option(string[] args, string short_name, string long_name) {
var short_idx = Array.IndexOf(args, short_name);
var long_idx = Array.IndexOf(args, long_name);
var idx = short_idx != -1 ? short_idx : long_idx != -1 ? long_idx : -1;
if (idx == -1)
return null;
if (long_name == "--help")
return idx != -1 ? "--help" : null;
if (idx + 1 >= args.Length)
throw new ArgumentException("Invalid option");
return args[idx + 1];
}
static string get_option(string[] args, string name) {
switch (name) {
case "-h":
case "--host":
return get_option(args, "-h", "--host");
case "-p":
case "--port":
return get_option(args, "-p", "--port");
case "-fr":
case "--from-recipients":
return get_option(args, "-fr", "--from-recipients");
case "-f":
case "--from":
return get_option(args, "-f", "--from");
case "-r":
case "--recipients":
return get_option(args, "-r", "--recipients");
case "-s":
case "--subject":
return get_option(args, "-s", "--subject");
case "-b":
case "--body":
return get_option(args, "-b", "--body");
case "--help":
return get_option(args, null, "--help");
default:
throw new ArgumentException("Unknown option: " + name);
}
}
static string str_or_test(string s) {
return s != null ? s : "Test";
}
static void print_usage() {
Console.WriteLine("Usage: sendmail_test -h 172.16.x.xxx -f [email protected] -r [email protected]");
}
static void print_help() {
Console.WriteLine("print_help");
}
static int Main(string[] args) {
if (args.Length == 0) {
print_usage();
return 0;
}
try {
if (get_option(args, "--help") != null) {
print_help();
return 0;
}
var host = get_option(args, "--host");
var port = get_option(args, "--port");
var from_recipients = get_option(args, "--from-recipients");
var from = get_option(args, "--from");
var recipients = get_option(args, "--recipients");
var subject = get_option(args, "--subject");
var body = get_option(args, "--body");
var sc = new SmtpClient();
sc.Host = host != null ? host : "localhost";
sc.Port = port != null ? int.Parse(port) : 25;
if (from_recipients != null) {
from = from_recipients;
recipients = from_recipients;
}
if (from == null || recipients == null) {
Console.WriteLine("from == null || recipients == null");
return 1;
}
sc.Send(from, recipients, str_or_test(subject), str_or_test(body));
sc.Dispose();
}
catch (ArgumentException e) {
Console.WriteLine("Error: " + e.Message);
return 1;
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment