Esempio n. 1
0
        public static void send(Message msg)
        {
            try
            {
                NetworkCredential credential = null;
                if (null != msg.session.getProperty("mail.smtp.user"))
                {
                    credential = new NetworkCredential(msg.session.getProperty("mail.smtp.user"), msg.session.getProperty("mail.smtp.password"));
                }
                String port = msg.session.getProperty("mail.smtp.port") != null?msg.session.getProperty("mail.smtp.port") : "25";

                SmtpClient smtp = new SmtpClient(msg.session.getProperty("mail.smtp.host"), int.Parse(port));
                smtp.Credentials = credential;
                MailMessage mail     = new MailMessage();
                MailAddress mailFrom = new MailAddress(msg.getFrom()[0].toString());
                mail.From = mailFrom;
                for (int i = 0; i < msg.getRecipients(Message.RecipientType.TO).Length; i++)
                {
                    MailAddress mailAddress = new MailAddress(msg.getRecipients(Message.RecipientType.TO)[i].ToString());
                    mail.To.Insert(i, mailAddress);
                }
                mail.Subject = msg.getSubject();
                if ("text/plain".Equals(msg.getContentType()))
                {
                    mail.Body = msg.getContent().ToString();
                }
                else
                {
                    throw new NotImplementedException();
                }
                mail.IsBodyHtml = ("text/html".Equals(msg.getContentType()));
                smtp.Send(mail);
            }
            catch (SmtpException se)
            {
                switch (se.StatusCode)
                {
                case SmtpStatusCode.GeneralFailure | SmtpStatusCode.MustIssueStartTlsFirst:
                    throw new javax.mail.SendFailedException(se.Message);

                default:
                    throw se;
                }
            }
        }
Esempio n. 2
0
 public static void send(Message msg)
 {
     try
     {
         NetworkCredential credential = null;
         if (null != msg.session.getProperty("mail.smtp.user"))
         {
             credential = new NetworkCredential(msg.session.getProperty("mail.smtp.user"), msg.session.getProperty("mail.smtp.password"));
         }
         String port = msg.session.getProperty("mail.smtp.port") != null ? msg.session.getProperty("mail.smtp.port") : "25";
         SmtpClient smtp = new SmtpClient(msg.session.getProperty("mail.smtp.host"), int.Parse(port));
         smtp.Credentials = credential;
         MailMessage mail = new MailMessage();
         MailAddress mailFrom = new MailAddress(msg.getFrom()[0].toString());
         mail.From = mailFrom;
         for (int i = 0; i < msg.getRecipients(Message.RecipientType.TO).Length; i++)
         {
             MailAddress mailAddress = new MailAddress(msg.getRecipients(Message.RecipientType.TO)[i].ToString());
             mail.To.Insert(i, mailAddress);
         }
         mail.Subject = msg.getSubject();
         if ("text/plain".Equals(msg.getContentType()))
         {
             mail.Body = msg.getContent().ToString();
         }
         else
         {
             throw new NotImplementedException();
         }
         mail.IsBodyHtml = ("text/html".Equals(msg.getContentType()));
         smtp.Send(mail);
     }
     catch (SmtpException se)
     {
         switch (se.StatusCode) {
             case SmtpStatusCode.GeneralFailure | SmtpStatusCode.MustIssueStartTlsFirst :
                 throw new javax.mail.SendFailedException (se.Message);
             default :
                 throw se;
         }
     }
 }