ラブびあ

ビール。ときどきラブ

xmlMail

Imports System.IO
Imports System.Xml
Imports System.Net.Mail
Imports System.Xml.Serialization
 
Module Main
    Dim smtp As New SmtpClient
 
    Function getText(ByRef node As XmlNode, ByRef def As String) As String
        If node Is Nothing OrElse node.InnerXml.Length = 0 Then
            Return def
        Else
            Return node.InnerXml
        End If
    End Function
 
    Sub deserializeMailMessage(ByRef filename As String, ByRef message As MailMessage)
        Dim xml As XmlDocument = New XmlDocument()
        xml.Load(filename)
 
        For Each node As XmlNode In xml.GetElementsByTagName("mailaddress")
            If node.InnerXml.Length = 0 Then
                Continue For
            End If
 
            Dim grp As XmlAttribute = node.Attributes("group")
            If grp Is Nothing Then
                Continue For
            End If
 
            Dim name As XmlAttribute = node.Attributes("displayname")
            Dim addr As MailAddress = If(name Is Nothing, _
                                         New MailAddress(node.InnerXml), _
                                         New MailAddress(node.InnerXml, name.Value))
            Select Case grp.Value.ToLower
                Case "from"
                    message.From = addr
                Case "sender"
                    message.Sender = addr
                Case "to"
                    message.To.Add(addr)
                Case "cc"
                    message.CC.Add(addr)
                Case "bcc"
                    message.Bcc.Add(addr)
            End Select
        Next
 
        message.Subject = getText(xml.SelectSingleNode("mailmessage/subject"), "no subject")
        message.Body = getText(xml.SelectSingleNode("mailmessage/body"), "no body")
 
        For Each node As XmlNode In xml.GetElementsByTagName("attachment")
            If File.Exists(node.InnerXml) Then
                message.Attachments.Add(New Attachment(node.InnerXml))
            End If
        Next
    End Sub
 
    Sub Main(ByVal args() As String)
        If args.Length = 0 Then
            Console.WriteLine("sendmail.exe [mailmessage.xml]")
            Console.WriteLine("    sendmail.exe sends e-mails with command-line-arguments.")
            Console.WriteLine("")
            Console.WriteLine("mailmessage.xml fileformat")
            Console.WriteLine("--------------")
            Console.WriteLine("<?xml version=""1.0"" encoding=""utf-8"" ?>")
            Console.WriteLine("<mailmessage>")
            Console.WriteLine("<mailaddress group=""to"" displayname=""addressee1"">")
            Console.WriteLine("to1@hoge.com")
            Console.WriteLine("</mailaddress>")
            Console.WriteLine("<mailaddress group=""to"" displayname=""addressee2"">")
            Console.WriteLine("to2@hoge.com")
            Console.WriteLine("</mailaddress>")
            Console.WriteLine("<subject>subject</subject>")
            Console.WriteLine("<body>body</body>")
            Console.WriteLine("<attachment>C:\temp\first.zip</attachment>")
            Console.WriteLine("<attachment>C:\temp\second.zip</attachment>")
            Console.WriteLine("<attachment>C:\temp\third.zip</attachment>")
            Console.WriteLine("</mailmessage>")
            Console.WriteLine("--------------")
            Console.WriteLine("<mailaddress>")
            Console.WriteLine("Attributes")
            Console.WriteLine("   group(required) [from/sender/to/cc/bcc]")
            Console.WriteLine("   displayname(optional)")
            Console.WriteLine("Value")
            Console.WriteLine("   fuga@hoge.com")
 
            Environment.Exit(0)
        End If
 
        Try
            For Each arg In args
                If File.Exists(arg) And _
                   ".xml".Equals(Path.GetExtension(arg), _
                                 StringComparison.CurrentCultureIgnoreCase) Then
                    Dim msg As New MailMessage
                    deserializeMailMessage(arg, msg)
                    smtp.Send(msg)
                    msg.Dispose()
                End If
            Next
 
            Environment.Exit(0)
        Catch ex As Exception
            Do Until ex Is Nothing
                Console.WriteLine(ex.Message)
                ex = ex.InnerException
            Loop
            Environment.Exit(9)
        End Try
    End Sub
End Module
</pre>

-sendmail.exe.config
<pre><?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <mailSettings>
      <smtp from="fuga@hoge.com">
        <network
          host="smtp.hoge.com"
          port="25"
          userName="xxx"
          password="xxx"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>