/** * 郵件監聽器 */ //private List<EmailSendListener> emailSendListeners = new ArrayList<EmailSendListener>(); public void init() { final Properties properties = SysConfig.getProperties(); this.theadPool = Executors.newFixedThreadPool(POOL_SIZE); this.session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties .getProperty("mail.smtp.username"), properties .getProperty("mail.smtp.password")); } }); username = properties.getProperty("mail.smtp.username"); //设置调试模式(输出调试信息) this.session.setDebug(false); }
public void setReadyForEmail(String username, String password) { props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); message = new MimeMessage(session); }
/** * * This method will create a new Mail Session * * @param smtpServer * Mail server IP/FQDN * @param port * SMTP port * @param sendFrom * Sender's Email ID * @param password * Email account Password * @param sendTo * Receiver's Email address * @return * Mail Session Object with Properties configured */ private static Session getSession(String smtpServer, String port, final String sendFrom, final String password) { Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); props.put("java.net.preferIPv4Stack", "true"); props.put("mail.smtp.port", port); Session session = Session.getInstance(props, null); if (!StringUtils.isBlank(password)) { props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sendFrom, password); } }); } return session; }
public boolean sendMsg(String recipient, String subject, String content) throws MessagingException { // Create a mail object Session session = Session.getInstance(props, new Authenticator() { // Set the account information session,transport will send mail @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(Constants.MAIL_USERNAME, Constants.MAIL_PASSWORD); } }); session.setDebug(true); Message msg = new MimeMessage(session); try { msg.setSubject(subject); //Set the mail subject msg.setContent(content,"text/html;charset=utf-8"); msg.setFrom(new InternetAddress(Constants.MAIL_USERNAME)); //Set the sender msg.setRecipient(RecipientType.TO, new InternetAddress(recipient)); //Set the recipient Transport.send(msg); return true; } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.getMessage()); return false; } }
public String sendMail() { mail.setPassword(Mailer.PA); mail.setHost(Mailer.HOST); mail.setSender(Mailer.SENDER); Properties properties = System.getProperties(); properties.put("mail.smtp.host", mail.getHost()); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.socketFactory.port", "465"); properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.port", "465"); Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAurhentication() { return new PasswordAuthentication(mail.getSender(), mail.getPassword()); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(mail.getSender())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail.getEmailId())); message.setSubject(mail.getSubject()); message.setText(mail.getMessage()); Transport.send(message, mail.getSender(),mail.getPassword()); System.out.println("Mail Sent"); return StatusCode.SUCCESS; } catch(Exception ex) { throw new RuntimeException("Error while sending mail" + ex); } }
@Test public void testSetSession() { smsTestClient.initializeSMSClient("Memoranda", "8005551212", "T-Mobile"); Session testSession; props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "outlook.office365.com"); props.put("mail.smtp.port", "587"); testSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smsTestClient.getUsername(), smsTestClient.getPassword()); } }); smsTestClient.setSession(testSession); }
private static void sendMail() throws MessagingException, IOException { Session session = Session.getInstance(getMailProps(), new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( getVal("username"), getVal("password")); } }); session.setDebug(getBoolVal("mail.debug")); LOG.info("Compiling Mail before Sending"); Message message = createMessage(session); Transport transport = session.getTransport("smtp"); LOG.info("Connecting to Mail Server"); transport.connect(); LOG.info("Sending Mail"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); LOG.info("Reports are sent to Mail"); clearTempZips(); }
/** * 获取用户与邮件服务器的连接 * * @param host 邮件主机名 * @param username 发件人的用户名 * @param password 发件人的用户名密码 * @return 返回指定用户与指定邮件服务器绑定的一个连接(会话) */ public static Session getSession(String host, final String username, final String password) { // 设置配置文件,邮件主机和是否认证 Properties property = new Properties(); property.put("mail.host", host); property.put("mail.smtp.auth", "true"); // 设置用户名和密码 Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // TODO Auto-generated method stub return new PasswordAuthentication(username, password); } }; // 获取与邮件主机的连接 Session session = Session.getInstance(property, auth); return session; }
private Session getMailSession(){ if(session==null){ Properties properties = new Properties(); properties.put("mail.smtp.host",smtpHost); if(StringUtils.isNotEmpty(smtpPort)){ properties.put("mail.smtp.port",Integer.parseInt(smtpPort)); } if(smtpIsAuth) { properties.put("mail.smtp.auth","true"); session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUser,smtpPassword); } }); } else { session = Session.getDefaultInstance(properties); } } return session; }
public static void sendMail(String host, int port, String username, String password, String recipients, String subject, String content, String from) throws AddressException, MessagingException { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); message.setSubject(subject); message.setText(content); Transport.send(message); }
public static Session authorizeWebShopEmail() throws MessagingException { Session session = null; final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); final String username = ApplicationProperties.SHOP_EMAIL; final String password = ApplicationProperties.SHOP_EMAIL_PASSWORD; session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); return session; }
/** * Creates a mail session from the underlying mail properties. * * @return the mail session. */ public Session createSession() { Session session; if (username == null) { session = Session.getInstance(properties); } else { Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password == null ? "" : password); } }; session = Session.getInstance(properties, auth); } return session; }
public PasswordAuthentication passwordAuthentication() { String userName=getDefaultUserName(); //当前用户在密码表里 if (pwdProperties.containsKey(userName)) { //取出密码,封装好后返回 return new PasswordAuthentication(userName, pwdProperties.getProperty(userName).toString()); } else { //如果当前用户不在密码表里就返回Null System.out.println("当前用户不存在"); return null; } }
public static Session getSenderSession(String host, final String user, final String password){ Session session = null; logger.info("Creating email session for sending, host: "+ host +", user: "+ user+ ", password: ******"); Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); return session; }
private static Session getReceiverSession(String host, final String user, final String password){ Session session = null; logger.info("Creating email session for receiving, host: "+ host +", user: "+ user+ ", password: ******"); Properties props = new Properties(); props.setProperty("mail.host", "imap.gmail.com"); props.setProperty("mail.port", "993"); props.setProperty("mail.transport.protocol", "imaps"); session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); return session; }
public static Session getSession(final SMTPConfig config) { // Create some properties and get the default Session. Properties props = System.getProperties(); props.put("mail.smtp.auth", config.isAuthentication()); props.put("mail.smtp.starttls.enable", config.isTlsenable()); props.put("mail.smtp.starttls.required", config.isTlsrequired()); props.put("mail.smtp.host", config.getHost()); props.put("mail.smtp.port", config.getPort()); Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(config.getUsername(), config.getPassword()); } }); return session; }
private Session createSession() { Properties props = new Properties(); props.put("mail.smtp.auth", SessionConfiguration.SMTP_AUTH); props.put("mail.smtp.starttls.enable", SessionConfiguration.SMTP_STARTTLS_ENABLE); props.put("mail.smtp.host", SessionConfiguration.SMTP_HOST); props.put("mail.smtp.port", greenmailService.getPort()); Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SessionConfiguration.SENDER_USERNAME, SessionConfiguration.SENDER_PASSWORD); } }); return session; }
public MailNotification(final String username, final String password) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); this.session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); }
/** * Getting the connection to email using Mail API * * @return - Email message Store * @throws ESBMailTransportIntegrationTestException - Is thrown if an error while connecting to email store */ private static Store getConnection() throws ESBMailTransportIntegrationTestException { Properties properties; Session session; properties = new Properties(); properties.setProperty("mail.host", "imap.gmail.com"); properties.setProperty("mail.port", "995"); properties.setProperty("mail.transport.protocol", "imaps"); session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(receiver + "@" + domain, String.valueOf(receiverPassword)); } }); try { Store store = session.getStore("imaps"); store.connect(); return store; } catch (MessagingException e) { log.error("Error when creating the email store ", e); throw new ESBMailTransportIntegrationTestException("Error when creating the email store ", e); } }
public static Session getSession() { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", "smtp.163.com"); props.setProperty("mail.smtp.port", "25"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String password = null; InputStream is = EmailUtils.class.getResourceAsStream("password.dat"); byte[] b = new byte[1024]; try { int len = is.read(b); password = new String(b,0,len); } catch (IOException e) { e.printStackTrace(); } return new PasswordAuthentication(FROM, password); } }); return session; }
public static Session getGMailSession(final String user, final String pass) { final Properties props = new Properties(); // Zum Empfangen props.setProperty("mail.store.protocol", "imaps"); // Zum Senden props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); return Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, pass); } }); }
public TokenMail(ResetPasswordToken token) throws UnsupportedEncodingException, MessagingException { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setSubject("Augmented password change"); message.setText(token.getToken()); }
/** * ��ȡ���ڷ����ʼ���Session * @return */ public static Session getSession() { Properties props = new Properties(); props.put("mail.smtp.host", HOST);//���÷�������ַ props.put("mail.store.protocol" , PROTOCOL);//����Э�� props.put("mail.smtp.port", PORT);//���ö˿� props.put("mail.smtp.auth" , true); Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SENDER, SENDERPWD); } }; Session session = Session.getDefaultInstance(props,authenticator); return session; }
private Session getMailSession() { Properties properties = new Properties(); properties.put("mail.smtp.auth", propertyService.getString("mail.smtp.auth")); properties.put("mail.smtp.starttls.enable", propertyService.getString("mail.smtp.starttls.enable")); properties.put("mail.smtp.host", propertyService.getString("mail.smtp.host")); properties.put("mail.smtp.port", propertyService.getString("mail.smtp.port")); Session session = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(propertyService.getString("mail.userName"), propertyService.getString("mail.password")); } }); return session; }
/** * Initialize the Mail Authenticator for entire application * * @throws Exception */ private void initMailAuthenticator() throws Exception { if(appConfig == null){ initAppConfig(); } final String userId = Application.getInstance().getAppConfig() .getSmtpUserId(); final String passwd = Application.getInstance().getAppConfig() .getSmtpPasswd(); auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userId, passwd); } }; }
GMailClient(Properties mailProperties) { final String username = mailProperties.getProperty("mail.smtp.username"); final String password = mailProperties.getProperty("mail.smtp.password"); log.info("Initializing gmail smtp mail transport. Username : {}. SMTP host : {}:{}", username, mailProperties.getProperty("mail.smtp.host"), mailProperties.getProperty("mail.smtp.port")); this.session = Session.getInstance(mailProperties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { this.from = new InternetAddress(username); } catch (AddressException e) { throw new RuntimeException("Error initializing MailWrapper." + e.getMessage()); } }
public void report(UserSetting userSetting, SearchResult searchResult, int resultState) { if(! (boolean)config.get("enableMailReport")) return; final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.user", config.get("fromEmailUser")); props.put("mail.password", config.get("fromEmailPassword")); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password")); } }; Session mailSession = Session.getInstance(props, authenticator); try { MimeMessage message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(props.getProperty("mail.user"))); message.setRecipient(RecipientType.TO, new InternetAddress(config.get("toEmailUser").toString())); message.setSubject("PTE助手通知"); String content = String.format("账号 %s 已搜索到可用约会: 时间=%s, 地点=%s", userSetting.user.username, CalendarUtils.chinese(searchResult.apptTime), searchResult.testCenter); if(resultState > -1) { content += "<br>"; content += resultState == 1 ? "并报名成功" : "但报名失败"; } message.setContent(content, "text/html;charset=UTF-8"); Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); } }
/** * 发送邮件 */ public boolean sendout() { try { mimeMsg.setContent(mp); mimeMsg.saveChanges(); logger.info(Resources.getMessage("EMAIL.SENDING")); Session mailSession = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { if (userkey == null || "".equals(userkey.trim())) { return null; } return new PasswordAuthentication(username, userkey); } }); Transport transport = mailSession.getTransport("smtp"); transport.connect((String) props.get("mail.smtp.host"), username, password); // 设置发送日期 mimeMsg.setSentDate(new Date()); // 发送 transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) { transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC)); } logger.info(Resources.getMessage("EMAIL.SEND_SUCC")); transport.close(); return true; } catch (Exception e) { logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e); return false; } }
/** * @desc 初始化邮件通用设置 * * @author liuliang * * @return MimeMessage * @throws MessagingException */ private MimeMessage initialMessage() throws MessagingException{ // 配置发送邮件的环境属性 final Properties props = new Properties(); // 表示SMTP发送邮件,需要进行身份验证 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", MAIL_SMTP_HOST); props.put("mail.smtp.port", MAIL_SMTP_PORT); // 发件人的账号 props.put("mail.user", emailSenderName); // 访问SMTP服务时需要提供的密码 props.put("mail.password", emailSenderPassword); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 MimeMessage message = new MimeMessage(mailSession); // 设置发件人 InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form); return message; }
/** * 发邮件处理 * * @param toAddr * 邮件地址 * @param content * 邮件内容 * @return 成功标识 */ public static boolean sendMail(String toAddr, String title, String content, boolean isHtmlFormat) { final String username = YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_USERNAME); final String password = YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_PASSWORD); Properties props = new Properties(); props.put("mail.smtp.auth", YiDuConstants.yiduConf.getBoolean(YiDuConfig.MAIL_SMTP_AUTH, true)); props.put("mail.smtp.starttls.enable", YiDuConstants.yiduConf.getBoolean(YiDuConfig.MAIL_SMTP_STARTTLS_ENABLE, true)); props.put("mail.smtp.host", YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_HOST)); props.put("mail.smtp.port", YiDuConstants.yiduConf.getInt(YiDuConfig.MAIL_SMTP_PORT, 25)); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_FROM))); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr)); message.setSubject(title); if (isHtmlFormat) { message.setContent(content, "text/html"); } else { message.setText(content); } Transport.send(message); } catch (MessagingException e) { logger.warn(e); return false; } return true; }
/** * 发送邮件 * * @param name String * @param pass String */ public boolean sendout() { try { mimeMsg.setContent(mp); mimeMsg.saveChanges(); logger.info(Resources.getMessage("EMAIL.SENDING")); Session mailSession = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { if (userkey == null || "".equals(userkey.trim())) { return null; } return new PasswordAuthentication(username, userkey); } }); Transport transport = mailSession.getTransport("smtp"); transport.connect((String) props.get("mail.smtp.host"), username, password); // 设置发送日期 mimeMsg.setSentDate(new Date()); // 发送 transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) { transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC)); } logger.info(Resources.getMessage("EMAIL.SEND_SUCC")); transport.close(); return true; } catch (Exception e) { logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e); return false; } }
public static void sendEmail(String host, String port, final String userName, final String password, String toAddress, String subject, String message, String nombreArchivoAdj, String urlArchivoAdj) throws AddressException, MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // creates a new session with an authenticator Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; Session session = Session.getInstance(properties, auth); //Se crea la parte del cuerpo del mensaje. BodyPart texto = new MimeBodyPart(); texto.setText(message); BodyPart adjunto = new MimeBodyPart(); if(nombreArchivoAdj != null ){ adjunto.setDataHandler(new DataHandler(new FileDataSource(urlArchivoAdj))); adjunto.setFileName(nombreArchivoAdj); } //Juntar las dos partes MimeMultipart multiParte = new MimeMultipart(); multiParte.addBodyPart(texto); if (nombreArchivoAdj != null) multiParte.addBodyPart(adjunto); // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = null; toAddresses = InternetAddress.parse(toAddress, false); msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); //msg.setText(message); msg.setContent(multiParte); // sends the e-mail Transport.send(msg); }
private void sendMail(String subject, String body, MailSettings settings) throws MessagingException { Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", settings.getSmtpServer()); props.put("mail.smtp.port", settings.getSmtpPort() + ""); if (settings.getMode() == MailSettings.Mode.SSL) { props.put("mail.smtp.socketFactory.port", settings.getSmtpPort() + ""); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); } else if (settings.getMode() == MailSettings.Mode.TLS) { props.put("mail.smtp.starttls.enable", "true"); } props.put("mail.smtp.auth", settings.isUseAuth() + ""); Session session; if (settings.isUseAuth()) { session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(settings.getUser(), settings.getPass()); } }); } else { session = Session.getInstance(props); } Message message = new MimeMessage(session); message.setFrom(new InternetAddress(settings.getFrom())); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(settings.getTo())); message.setSubject(subject); message.setText(body); Transport.send(message); }
public static void sendEmail(EmailSettingContent emailSetting, String acceptor, String subject, String body) { Properties props = buildProperty(emailSetting); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String username = null; String password = null; if (emailSetting.isAuthenticated()) { username = emailSetting.getUsername(); password = emailSetting.getPassword(); } return new PasswordAuthentication(username, password); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(emailSetting.getSender())); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(acceptor)); message.setSubject(subject, "utf8"); message.setContent(body, "text/html;charset=utf8"); Transport.send(message); } catch (Throwable throwable) { } }