예제 #1
0
		/// <summary>
		/// 以文本格式发送邮件
		/// </summary>
		/// <param name="mailInfo"> 待发送的邮件的信息 </param>
		public static bool sendTextMail(MailSenderInfo mailInfo)
		{

			try
			{

				// 设置一些通用的数据
				Message mailMessage = setCommon(mailInfo);

				// 设置邮件消息的主要内容
				string mailContent = mailInfo.Content;
				mailMessage.Text = mailContent;

				// 发送邮件
				Transport.send(mailMessage);

				return true;

			}
			catch (MessagingException ex)
			{

				Console.WriteLine(ex.ToString());
				Console.Write(ex.StackTrace);
			}

			return false;
		}
예제 #2
0
		/// <param name="mailInfo">
		/// 
		/// @return </param>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private static javax.mail.Message setCommon(MailSenderInfo mailInfo) throws javax.mail.MessagingException
		private static Message setCommon(MailSenderInfo mailInfo)
		{

			//
			// 判断是否需要身份认证
			//
			MyAuthenticator authenticator = null;
			Properties pro = mailInfo.Properties;
			if (mailInfo.Validate)
			{
				// 如果需要身份认证,则创建一个密码验证器
				authenticator = new MyAuthenticator(mailInfo.UserName, mailInfo.Password);
			}

			// 根据邮件会话属性和密码验证器构造一个发送邮件的session
			Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);

			// 创建邮件发送者地址
			Address from = new InternetAddress(mailInfo.FromAddress);

			// 设置邮件消息的发送者
			mailMessage.From = from;

			// 创建邮件的接收者地址,并设置到邮件消息中
			Address to = new InternetAddress(mailInfo.ToAddress);
			mailMessage.setRecipient(Message.RecipientType.TO, to);

			// 设置邮件消息的主题
			mailMessage.Subject = mailInfo.Subject;

			// 设置邮件消息发送的时间
			mailMessage.SentDate = DateTime.Now;

			return mailMessage;

		}
예제 #3
0
		/// <summary>
		/// 以HTML格式发送邮件
		/// </summary>
		/// <param name="mailInfo"> 待发送的邮件信息 </param>
		public static bool sendHtmlMail(MailSenderInfo mailInfo)
		{

			try
			{

				LOG.info("send to " + mailInfo.FromAddress);

				// 设置一些通用的数据
				Message mailMessage = setCommon(mailInfo);

				// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
				Multipart mainPart = new MimeMultipart();

				// 创建一个包含HTML内容的MimeBodyPart
				BodyPart html = new MimeBodyPart();

				// 设置HTML内容
				html.setContent(mailInfo.Content, "text/html; charset=utf-8");
				mainPart.addBodyPart(html);

				// 将MiniMultipart对象设置为邮件内容
				mailMessage.Content = mainPart;

				// 发送邮件
				Transport.send(mailMessage);

				LOG.info("send to " + mailInfo.FromAddress + " dnoe.");

				return true;

			}
			catch (MessagingException ex)
			{

				LOG.error(ex.ToString(), ex);
			}
			return false;
		}