use of javax.mail.Session in project KJFrameForAndroid by kymjs.
the class SimpleMailSender method sendTextMail.
/**
* 以文本格式发送邮件
*
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
use of javax.mail.Session in project zoj by licheng.
the class EmailService method sendEmail.
public static void sendEmail(String email, String title, String replyTo, String content) throws Exception {
String smtpUser = ConfigManager.getValue("smtp_user");
String smtpPassword = ConfigManager.getValue("smtp_password");
String smtpHost = ConfigManager.getValue("smtp_host");
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
if (smtpUser != null && smtpUser.length() > 0) {
props.put("mail.smtp.auth", "true");
}
Session sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(replyTo));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
newMessage.setSubject(title);
newMessage.setSentDate(new Date());
newMessage.setText(content);
Transport trans = sendMailSession.getTransport("smtp");
if (smtpUser != null && smtpUser.length() > 0) {
trans.connect(smtpHost, smtpUser, smtpPassword);
} else {
trans.connect();
}
trans.sendMessage(newMessage, newMessage.getRecipients(javax.mail.Message.RecipientType.TO));
trans.close();
}
use of javax.mail.Session in project jodd by oblac.
the class SmtpServer method createSession.
/**
* {@inheritDoc}
*/
public SendMailSession createSession() {
Properties sessionProperties = createSessionProperties();
if (additionalProperties != null) {
sessionProperties.putAll(additionalProperties);
}
Session mailSession = Session.getInstance(sessionProperties, authenticator);
Transport mailTransport;
try {
mailTransport = getTransport(mailSession);
} catch (NoSuchProviderException nspex) {
throw new MailException(nspex);
}
return new SendMailSession(mailSession, mailTransport);
}
use of javax.mail.Session in project jodd by oblac.
the class ImapServer method createSession.
/**
* {@inheritDoc}
*/
public ReceiveMailSession createSession() {
Session session = Session.getInstance(sessionProperties, authenticator);
Store store;
try {
store = getStore(session);
} catch (NoSuchProviderException nspex) {
throw new MailException("Failed to create IMAP session", nspex);
}
return new ReceiveMailSession(session, store);
}
use of javax.mail.Session in project camel by apache.
the class MimeMultipartDataFormat method marshal.
@Override
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws NoTypeConversionAvailableException, MessagingException, IOException {
if (multipartWithoutAttachment || headersInline || exchange.getIn().hasAttachments()) {
ContentType contentType = getContentType(exchange);
// remove the Content-Type header. This will be wrong afterwards...
exchange.getOut().removeHeader(Exchange.CONTENT_TYPE);
byte[] bodyContent = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, graph);
Session session = Session.getInstance(System.getProperties());
MimeMessage mm = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart(multipartSubType);
BodyPart part = new MimeBodyPart();
writeBodyPart(bodyContent, part, contentType);
mp.addBodyPart(part);
for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
String attachmentFilename = entry.getKey();
Attachment attachment = entry.getValue();
part = new MimeBodyPart();
part.setDataHandler(attachment.getDataHandler());
part.setFileName(MimeUtility.encodeText(attachmentFilename, "UTF-8", null));
String ct = attachment.getDataHandler().getContentType();
contentType = new ContentType(ct);
part.setHeader(CONTENT_TYPE, ct);
if (!contentType.match("text/*") && binaryContent) {
part.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
}
// Set headers to the attachment
for (String headerName : attachment.getHeaderNames()) {
List<String> values = attachment.getHeaderAsList(headerName);
for (String value : values) {
part.setHeader(headerName, value);
}
}
mp.addBodyPart(part);
exchange.getOut().removeAttachment(attachmentFilename);
}
mm.setContent(mp);
// a String
if (headersInline && includeHeaders != null) {
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (includeHeaders.matcher(entry.getKey()).matches()) {
String headerStr = ExchangeHelper.convertToType(exchange, String.class, entry.getValue());
if (headerStr != null) {
mm.setHeader(entry.getKey(), headerStr);
}
}
}
}
mm.saveChanges();
Enumeration<?> hl = mm.getAllHeaders();
List<String> headers = new ArrayList<String>();
if (!headersInline) {
while (hl.hasMoreElements()) {
Object ho = hl.nextElement();
if (ho instanceof Header) {
Header h = (Header) ho;
exchange.getOut().setHeader(h.getName(), h.getValue());
headers.add(h.getName());
}
}
mm.saveChanges();
}
mm.writeTo(stream, headers.toArray(new String[0]));
} else {
// keep the original data
InputStream is = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
IOHelper.copyAndCloseInput(is, stream);
}
}
Aggregations