use of javax.mail.Transport in project remusic by aa112901.
the class CommonUtils method sendTextMail.
/**
* 以文本格式发送邮件
* @param title 待发送的邮件的信息
*/
public static boolean sendTextMail(String title, String content) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", 25, "remusic_log@163.com", "remusiclog1");
Message mailMessage = new SMTPMessage(session);
Address from = new InternetAddress("remusic_log@163.com");
mailMessage.setFrom(from);
Address to = new InternetAddress("remusic_log@163.com");
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(title);
mailMessage.setSentDate(new Date());
mailMessage.setText(content);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
use of javax.mail.Transport in project javaee7-samples by javaee-samples.
the class AnnotatedEmailServlet method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Sending email using @MailSessionDefinition</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Sending email using @MailSessionDefinition</h1>");
try {
out.println("Sending message from \"" + creds.getFrom() + "\" to \"" + creds.getTo() + "\"...<br>");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(creds.getFrom()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(creds.getTo()));
message.setSubject("Sending message using Annotated JavaMail " + Calendar.getInstance().getTime());
message.setText("Java EE 7 is cool!");
Transport t = session.getTransport();
t.connect(creds.getFrom(), creds.getPassword());
t.sendMessage(message, message.getAllRecipients());
out.println("message sent!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
out.println("</body>");
out.println("</html>");
}
}
use of javax.mail.Transport in project jforum2 by rafaelsteil.
the class Spammer method dispatchMessages.
public boolean dispatchMessages() {
try {
int sendDelay = SystemGlobals.getIntValue(ConfigKeys.MAIL_SMTP_DELAY);
if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) {
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
boolean ssl = SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_SSL);
Transport transport = this.session.getTransport(ssl ? "smtps" : "smtp");
try {
String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
transport.connect(host, username, password);
if (transport.isConnected()) {
for (Iterator userIter = this.users.iterator(); userIter.hasNext(); ) {
User user = (User) userIter.next();
if (this.needCustomization) {
this.defineUserMessage(user);
}
Address address = new InternetAddress(user.getEmail());
logger.debug("Sending mail to: " + user.getEmail());
this.message.setRecipient(Message.RecipientType.TO, address);
transport.sendMessage(this.message, new Address[] { address });
if (sendDelay > 0) {
try {
Thread.sleep(sendDelay);
} catch (InterruptedException ie) {
logger.error("Error while Thread.sleep." + ie, ie);
}
}
}
}
} catch (Exception e) {
throw new MailException(e);
} finally {
try {
transport.close();
} catch (Exception e) {
}
}
}
} else {
for (Iterator iter = this.users.iterator(); iter.hasNext(); ) {
User user = (User) iter.next();
if (this.needCustomization) {
this.defineUserMessage(user);
}
Address address = new InternetAddress(user.getEmail());
logger.debug("Sending mail to: " + user.getEmail());
this.message.setRecipient(Message.RecipientType.TO, address);
Transport.send(this.message, new Address[] { address });
if (sendDelay > 0) {
try {
Thread.sleep(sendDelay);
} catch (InterruptedException ie) {
logger.error("Error while Thread.sleep." + ie, ie);
}
}
}
}
} catch (MessagingException e) {
logger.error("Error while dispatching the message." + e, e);
}
return true;
}
use of javax.mail.Transport in project spring-framework by spring-projects.
the class JavaMailSenderImpl method connectTransport.
/**
* Obtain and connect a Transport from the underlying JavaMail Session,
* passing in the specified host, port, username, and password.
* @return the connected Transport object
* @throws MessagingException if the connect attempt failed
* @since 4.1.2
* @see #getTransport
* @see #getHost()
* @see #getPort()
* @see #getUsername()
* @see #getPassword()
*/
protected Transport connectTransport() throws MessagingException {
String username = getUsername();
String password = getPassword();
if ("".equals(username)) {
// probably from a placeholder
username = null;
if ("".equals(password)) {
// in conjunction with "" username, this means no password to use
password = null;
}
}
Transport transport = getTransport(getSession());
transport.connect(getHost(), getPort(), username, password);
return transport;
}
use of javax.mail.Transport in project gocd by gocd.
the class GoSmtpMailSender method send.
public ValidationBean send(String subject, String body, String to) {
Transport transport = null;
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Sending email [%s] to [%s]", subject, to));
}
Properties props = mailProperties();
MailSession session = MailSession.getInstance().createWith(props, username, password);
transport = session.getTransport();
transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password));
MimeMessage msg = session.createMessage(from, to, subject, body);
transport.sendMessage(msg, msg.getRecipients(TO));
return ValidationBean.valid();
} catch (Exception e) {
LOGGER.error(String.format("Sending failed for email [%s] to [%s]", subject, to), e);
return ValidationBean.notValid(ERROR_MESSAGE);
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
LOGGER.error("Failed to close transport", e);
}
}
}
}
Aggregations