Java Code Examples for org.apache.commons.mail.HtmlEmail#setAuthentication()

The following examples show how to use org.apache.commons.mail.HtmlEmail#setAuthentication() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailSenderDefaultImpl.java    From minsx-framework with Apache License 2.0 7 votes vote down vote up
public void finishInitial() {
    try {
        Integer sslPort = 465;
        simpleEmail = new SimpleEmail();
        simpleEmail.setHostName(this.host);
        simpleEmail.setSmtpPort(this.port);
        simpleEmail.setAuthentication(this.username, this.password);
        simpleEmail.setFrom(this.from);
        simpleEmail.setCharset("UTF-8");
        if (sslPort.equals(this.port)) {
            simpleEmail.setSSLOnConnect(true);
        }
        htmlEmail = new HtmlEmail();
        htmlEmail.setHostName(this.host);
        htmlEmail.setSmtpPort(this.port);
        htmlEmail.setAuthentication(this.username, this.password);
        htmlEmail.setFrom(this.from);
        htmlEmail.setCharset("UTF-8");
        if (sslPort.equals(this.port)) {
            htmlEmail.setSSLOnConnect(true);
        }
    } catch (EmailException e) {
        throw new MailSendException(String.format("Incorrect setting field of [from],cause: %s", e.getMessage()), e);
    }
}
 
Example 2
Source File: SendMailUtil.java    From jeewx-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 发送普通邮件
 * @param toMailAddr 收信人地址
 * @param subject email主题
 * @param message 发送email信息  
 */
public static boolean sendCommonMail(String toMailAddr, String subject, String message) {
	  HtmlEmail hemail = new HtmlEmail();
	try {
		SystemProperties systemProperties = ApplicationContextUtil.getBean(SystemProperties.class);
		hemail.setHostName(systemProperties.getSmtpHost());
		hemail.setSmtpPort(getSmtpPort(systemProperties.getSender()));
		hemail.setCharset(charSet);
		hemail.addTo(toMailAddr);
		hemail.setFrom(systemProperties.getSender(), fromName);
		hemail.setAuthentication(systemProperties.getUser(), systemProperties.getPwd());
		hemail.setSubject(subject);
		hemail.setMsg(message);
		hemail.send();
		log.info("send email to "+toMailAddr+" OK!");
		return true;
	} catch (Exception e) {
	      e.printStackTrace();
	      log.info("send email to "+toMailAddr+" error!"+ e.toString());
	      return false;
	}
}
 
Example 3
Source File: EmailHandler.java    From robozonky with Apache License 2.0 6 votes vote down vote up
private HtmlEmail createNewEmail(final SessionInfo session) throws EmailException {
    final HtmlEmail email = new HtmlEmail();
    email.setCharset(Defaults.CHARSET.displayName()); // otherwise the e-mail contents are mangled
    email.setHostName(getSmtpHostname());
    email.setSmtpPort(getSmtpPort());
    email.setStartTLSRequired(isStartTlsRequired());
    email.setSSLOnConnect(isSslOnConnectRequired());
    if (isAuthenticationRequired()) {
        final String username = getSmtpUsername();
        LOGGER.debug("Will contact SMTP server as '{}'.", username);
        email.setAuthentication(getSmtpUsername(), getSmtpPassword());
    } else {
        LOGGER.debug("Will contact SMTP server anonymously.");
    }
    email.setFrom(getSender(), "RoboZonky '" + session.getName() + "'");
    email.addTo(getRecipient());
    return email;
}
 
Example 4
Source File: MailUtil.java    From JARVIS with MIT License 6 votes vote down vote up
public void send(Mail mail) throws EmailException {  
    // ����email  
    HtmlEmail email = new HtmlEmail();  
    // ������SMTP���ͷ����������֣�163�����£�"smtp.163.com"  
    email.setHostName(mail.getHost());  
    // �ַ����뼯������  
    email.setCharset(Mail.ENCODEING);  
    // �ռ��˵�����  
    email.addTo(mail.getReceiver());  
    // �����˵�����  
    email.setFrom(mail.getSender(), mail.getName());  
    // �����Ҫ��֤��Ϣ�Ļ���������֤���û���-���롣�ֱ�Ϊ���������ʼ��������ϵ�ע�����ƺ�����  
    email.setAuthentication(mail.getUsername(), mail.getPassword());  
    // Ҫ���͵��ʼ�����  
    email.setSubject(mail.getSubject());  
    // Ҫ���͵���Ϣ������ʹ����HtmlEmail���������ʼ�������ʹ��HTML��ǩ  
    email.setMsg(mail.getMessage());  
    //��Ӹ���
    if(mail.getAttachment()!=null)
    	email.attach(mail.getAttachment());
    // ����  
    email.send();  
    System.out.println(mail.getSender() + " �����ʼ��� " + mail.getReceiver());  
}
 
Example 5
Source File: SendMailUtil.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 发送普通邮件
 * 
 * @param toMailAddr
 *            收信人地址
 * @param subject
 *            email主题
 * @param message
 *            发送email信息
 */
public static void sendCommonMail(String toMailAddr, String subject,
		String message) {
	HtmlEmail hemail = new HtmlEmail();
	try {
		hemail.setHostName(getHost(from));
		hemail.setSmtpPort(getSmtpPort(from));
		hemail.setCharset(charSet);
		hemail.addTo(toMailAddr);
		hemail.setFrom(from, fromName);
		hemail.setAuthentication(username, password);
		hemail.setSubject(subject);
		hemail.setMsg(message);
		hemail.send();
		System.out.println("email send true!");
	} catch (Exception e) {
		e.printStackTrace();
		System.out.println("email send error!");
	}

}
 
Example 6
Source File: SendMailUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 发送普通邮件
 * @param toMailAddr 收信人地址
 * @param subject email主题
 * @param message 发送email信息  
 */
public static void sendCommonMail(String toMailAddr, String subject, String message) {
	  HtmlEmail hemail = new HtmlEmail();
	try {
		hemail.setHostName(getHost(from));
		hemail.setSmtpPort(getSmtpPort(from));
		hemail.setCharset(charSet);
		hemail.addTo(toMailAddr);
		hemail.setFrom(from, fromName);
		hemail.setAuthentication(username, password);
		hemail.setSubject(subject);
		hemail.setMsg(message);
		hemail.send();
		org.jeecgframework.core.util.LogUtil.info("email send true!");
	} catch (Exception e) {
	      e.printStackTrace();
	      org.jeecgframework.core.util.LogUtil.info("email send error!");
	    }
	
}
 
Example 7
Source File: SendMailUtil.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/**
 * 发送普通邮件
 * @param toMailAddr 收信人地址
 * @param subject email主题
 * @param message 发送email信息  
 */
public static void sendCommonMail(String toMailAddr, String subject, String message) {
	  HtmlEmail hemail = new HtmlEmail();
	try {
		hemail.setHostName(getHost(from));
		hemail.setSmtpPort(getSmtpPort(from));
		hemail.setCharset(charSet);
		hemail.addTo(toMailAddr);
		hemail.setFrom(from, fromName);
		hemail.setAuthentication(username, password);
		hemail.setSubject(subject);
		hemail.setMsg(message);
		hemail.send();
		org.jeecgframework.core.util.LogUtil.info("email send true!");
	} catch (Exception e) {
	      e.printStackTrace();
	      org.jeecgframework.core.util.LogUtil.info("email send error!");
	    }
	
}
 
Example 8
Source File: EmailUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 发送邮件
 * @param toAddress 接收地址
 * @param subject 标题
 * @param content 内容
 * @return
 */
public static boolean send(String fromAddress, String fromPassword, String fromHostName,
		String sslOnConnect, String sslSmtpPort, String toAddress, String subject, String content) {
	try {
		HtmlEmail htmlEmail = new HtmlEmail();
		// 发送地址
		htmlEmail.setFrom(fromAddress);
		// 密码校验
		htmlEmail.setAuthentication(fromAddress, fromPassword);
		// 发送服务器协议
		htmlEmail.setHostName(fromHostName);

		// SSL
		if ("true".equals(sslOnConnect)) {
			htmlEmail.setSSLOnConnect(true);
			htmlEmail.setSslSmtpPort(sslSmtpPort);
		}

		// 接收地址
		htmlEmail.addTo(toAddress);

		// 标题
		htmlEmail.setSubject(subject);
		// 内容
		htmlEmail.setMsg(content);

		// 其他信息
		htmlEmail.setCharset("utf-8");
		
		// 发送
		htmlEmail.send();
		return true;
	} catch (Exception ex) {
		logger.error(ex.getMessage(), ex);
	}
	return false;
}
 
Example 9
Source File: SendMailSsl.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link HtmlEmail} object configured as per the AuthMe config
 * with the given email address as recipient.
 *
 * @param emailAddress the email address the email is destined for
 * @return the created HtmlEmail object
 * @throws EmailException if the mail is invalid
 */
public HtmlEmail initializeMail(String emailAddress) throws EmailException {
    String senderMail = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_ADDRESS))
        ? settings.getProperty(EmailSettings.MAIL_ACCOUNT)
        : settings.getProperty(EmailSettings.MAIL_ADDRESS);

    String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
        ? senderMail
        : settings.getProperty(EmailSettings.MAIL_SENDER_NAME);
    String mailPassword = settings.getProperty(EmailSettings.MAIL_PASSWORD);
    int port = settings.getProperty(EmailSettings.SMTP_PORT);

    HtmlEmail email = new HtmlEmail();
    email.setCharset(EmailConstants.UTF_8);
    email.setSmtpPort(port);
    email.setHostName(settings.getProperty(EmailSettings.SMTP_HOST));
    email.addTo(emailAddress);
    email.setFrom(senderMail, senderName);
    email.setSubject(settings.getProperty(EmailSettings.RECOVERY_MAIL_SUBJECT));
    email.setAuthentication(settings.getProperty(EmailSettings.MAIL_ACCOUNT), mailPassword);
    if (settings.getProperty(PluginSettings.LOG_LEVEL).includes(LogLevel.DEBUG)) {
        email.setDebug(true);
    }

    setPropertiesForPort(email, port);
    return email;
}
 
Example 10
Source File: SendEmaiWithGmail.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
/**
 * 发送 html 格式的邮件
 * 
 * @param userName
 * @param password
 * @param subject
 * @param from
 * @param to
 * @param cc
 * @param bcc
 * @throws EmailException
 * @throws java.net.MalformedURLException
 */
public void sendHTMLEmail(String userName, String password, String subject,
		String from, String to, String cc, String bcc)
		throws EmailException, MalformedURLException {

	// 创建SimpleEmail对象
	HtmlEmail email = new HtmlEmail();

	// 显示调试信息用于IED中输出
	email.setDebug(true);

	// 设置发送电子邮件的邮件服务器
	email.setHostName("smtp.gmail.com");

	// 邮件服务器是否使用ssl加密方式gmail就是,163就不是)
	email.setSSL(Boolean.TRUE);

	// 设置smtp端口号(需要查看邮件服务器的说明ssl加密之后端口号是不一样的)
	email.setSmtpPort(465);

	// 设置发送人的账号/密码
	email.setAuthentication(userName, password);

	// 显示的发信人地址,实际地址为gmail的地址
	email.setFrom(from);
	// 设置发件人的地址/称呼
	// email.setFrom("[email protected]", "发送人");

	// 收信人地址
	email.addTo(to);
	// 设置收件人的账号/称呼)
	// email.addTo("[email protected]", "收件人");

	// 多个抄送地址
	StrTokenizer stokenCC = new StrTokenizer(cc.trim(), ";");
	// 开始逐个抄送地址
	for (int i = 0; i < stokenCC.getTokenArray().length; i++) {
		email.addCc((String) stokenCC.getTokenArray()[i]);
	}

	// 多个密送送地址
	StrTokenizer stokenBCC = new StrTokenizer(bcc.trim(), ";");
	// 开始逐个抄送地址
	for (int i = 0; i < stokenBCC.getTokenArray().length; i++) {
		email.addBcc((String) stokenBCC.getTokenArray()[i]);
	}

	// Set the charset of the message.
	email.setCharset("UTF-8");

	email.setSentDate(new Date());

	// 设置标题,但是不能设置编码,commons 邮件的缺陷
	email.setSubject(subject);

	// === 以上同 simpleEmail

	// ===html mail 内容
	StringBuffer msg = new StringBuffer();
	msg.append("<html><body>");
	// embed the image and get the content id

	// 远程图片
	URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
	String cid = email.embed(url, "Apache logo");
	msg.append("<img src=\"cid:").append(cid).append("\">");

	// 本地图片
	File img = new File("d:/java.gif");
	msg.append("<img src=cid:").append(email.embed(img)).append(">");
	msg.append("</body></html>");

	// === html mail 内容

	// ==== // set the html message
	email.setHtmlMsg(msg.toString());
	// set the alternative message
	email.setTextMsg("Your email client does not support HTML messages");
	// send the email
	email.send();
	System.out.println("The HtmlEmail send sucessful!");

}