Search in sources :

Example 56 with Session

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Example 57 with Session

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();
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Transport(javax.mail.Transport) Date(java.util.Date) Session(javax.mail.Session)

Example 58 with Session

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);
}
Also used : Properties(java.util.Properties) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 59 with Session

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);
}
Also used : Store(javax.mail.Store) NoSuchProviderException(javax.mail.NoSuchProviderException) Session(javax.mail.Session)

Example 60 with Session

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);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) ContentType(javax.mail.internet.ContentType) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map) Session(javax.mail.Session)

Aggregations

Session (javax.mail.Session)110 MimeMessage (javax.mail.internet.MimeMessage)72 Properties (java.util.Properties)66 InternetAddress (javax.mail.internet.InternetAddress)49 MessagingException (javax.mail.MessagingException)47 Message (javax.mail.Message)31 Test (org.junit.Test)30 Transport (javax.mail.Transport)28 JMSession (com.zimbra.cs.util.JMSession)20 PasswordAuthentication (javax.mail.PasswordAuthentication)19 Date (java.util.Date)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 MimeMultipart (javax.mail.internet.MimeMultipart)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)15 IOException (java.io.IOException)15 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)15 Authenticator (javax.mail.Authenticator)12 Multipart (javax.mail.Multipart)11 NoSuchProviderException (javax.mail.NoSuchProviderException)10 BodyPart (javax.mail.BodyPart)9