Esempio n. 1
0
		private MailMessage CreateMailMessage(DeliveryMessagePo deliveryMessage)
		{
		    MailMessage mailMessage;
            if (string.IsNullOrEmpty(deliveryMessage.EmlFile))
		    {
                mailMessage = new MailMessage(CreateSenderMailAddress(new MailAddress(deliveryMessage.Delivery.Sender.Address, deliveryMessage.Delivery.Sender.DisplayName), DefaultFrom, DefaultFromDisplayName, true), new MailAddress(deliveryMessage.To.Address, deliveryMessage.To.DisplayName))
                {
                    Subject = deliveryMessage.Subject,
                    Body = deliveryMessage.Body,
                    IsBodyHtml = true,
                    BodyEncoding = Encoding.UTF8,

                    //lascio il subject così altrimenti se ci sono lettere strane poi viene una merda: http://stackoverflow.com/questions/454833/system-net-mail-and-utf-8bxxxxx-headers
                    SubjectEncoding = Encoding.GetEncoding("iso-8859-1")
                };

                foreach (var item in CreateMailAddressCollection(DefaultBcc, deliveryMessage.Delivery.Bcc, false))
                    mailMessage.Bcc.Add(item);
                foreach (var item in CreateMailAddressCollection(DefaultCC, deliveryMessage.Delivery.Cc, false))
                    mailMessage.CC.Add(item);
		    }
            else
            {
                mailMessage = MailMessage.Load(deliveryMessage.EmlFile, MailMessageLoadOptions.DefaultEml);
            }

			return mailMessage;
		}
Esempio n. 2
0
		private SendMailEventArgs SendMail(DeliveryMessagePo deliveryMessage)
		{
			var message = CreateMailMessage(deliveryMessage);
		    if (message.To.Count > 0 || message.CC.Count > 0 || message.Bcc.Count > 0)
		    {
		        try
		        {
		            if (UseAuth)
		            {
		                Smtpclient.Username = SmtpLogin.UserName;
		                Smtpclient.Password = SmtpLogin.Password;
		                _log.DebugFormat("Using credential UserName:{0} Password:{1}", SmtpLogin.UserName, SmtpLogin.Password);
		            }

		            Smtpclient.Send(message);
		            _log.Debug("Send mail sync");
		            IsBusy = false;

		            var sendMailEventArgs = new SendMailEventArgs(message, SmtpServer, null);
		            OnMailSent(sendMailEventArgs);

		            return sendMailEventArgs;
		        }
		        catch (SmtpException ex)
		        {
		            var destinatario = string.Join(",", message.To) + string.Join(",", message.CC) + string.Join(",", message.Bcc);
		            var exmessage = $"Srv:{SmtpServer}{Environment.NewLine}UseAuth:{UseAuth}{Environment.NewLine}AuthMthd:{Smtpclient.AuthenticationMethod}{Environment.NewLine}Usr:{Smtpclient.Username}{Environment.NewLine}Pwd:{Smtpclient.Password}{Environment.NewLine}F:{message.From}{Environment.NewLine}T:{destinatario}{Environment.NewLine}Sbj:{message.Subject}{Environment.NewLine}Msg:{message.Body}";

		            var backupSent = false;
		            if (UseBackupServer)
		            {
		                _log.WarnFormat("Using SmtpBackupServer: {0}", ex, exmessage);
		                backupSent = SendMailByBackupServer(message);
		            }
		            else
		            {
		                IsBusy = false;
		                _log.Error(exmessage, ex);
		            }

		            if (!backupSent)
		            {
		                var sendMailEventArgs = new SendMailEventArgs(message, null, ex);

		                if (ThrowException)
		                    throw new Exception(exmessage, ex);
		                else
		                    OnMailNotSent(sendMailEventArgs);

		                return sendMailEventArgs;
		            }
		            else
		            {
		                var sendMailEventArgs = new SendMailEventArgs(message, Config.BackupSmtpServer, null);
		                OnMailSent(sendMailEventArgs);
		                return sendMailEventArgs;
		            }
		        }
		        finally
		        {
		            message.Dispose();
		        }
		    }
		    else
		    {
                return new SendMailEventArgs(null, null, new InvalidDataException("Non è presente nessun destinatario"));
            }
		}