Search in sources :

Example 16 with Transport

use of javax.mail.Transport in project zm-mailbox by Zimbra.

the class MailSender method checkMTAConnectionToHost.

private void checkMTAConnectionToHost(String hostname) throws MessagingException {
    mSession.getProperties().setProperty("mail.smtp.host", hostname);
    if (mEnvelopeFrom != null) {
        mSession.getProperties().setProperty("mail.smtp.from", mEnvelopeFrom);
    }
    ZimbraLog.smtp.debug("Testing connection to SMTP host %s with properties: %s", hostname, mSession.getProperties());
    Transport transport = mSession.getTransport("smtp");
    try {
        transport.connect();
    } finally {
        transport.close();
    }
}
Also used : Transport(javax.mail.Transport)

Example 17 with Transport

use of javax.mail.Transport in project smartmodule by carozhu.

the class JavaEmail method sendEmailBUGForDevloper.

public static void sendEmailBUGForDevloper(String formApp, Throwable e, String email, String emailPassword) throws AddressException, MessagingException {
    Properties properties = new Properties();
    // Send mail	// protocol
    properties.setProperty("mail.transport.protocol", "smtp");
    // Need to verify
    properties.setProperty("mail.smtp.auth", "true");
    //Background debug mode
    properties.setProperty("mail.debug", "true");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.host", "smtp.qq.com");
    properties.put("mail.smtp.port", "220");
    MyAuthenticator smyauth = new MyAuthenticator(email, emailPassword);
    // setup process output messages sent
    Session session = Session.getInstance(properties, smyauth);
    // debug mode
    session.setDebug(true);
    // Email message
    Message messgae = new MimeMessage(session);
    // set sender
    messgae.setFrom(new InternetAddress(email));
    // set the messgae content
    messgae.setText(getErrorInfoFromException(e));
    System.out.println(getErrorInfoFromException(e));
    Date now = new Date();
    //可以方便地修改日期格式
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String hehe = dateFormat.format(now);
    // set the message subject
    messgae.setSubject("author:caro-->recv" + formApp + "   异常反馈,时间:" + hehe);
    // Send e-mail
    Transport tran = session.getTransport();
    // tran.connect("smtp.sohu.com", 25, "xxx@sohu.com", "xxxx");//sohu-mail
    // server to connect to
    // tran.connect("smtp.sina.com", 25, "xxx@sina.cn",
    // "xxxxxxx");//Sina-mail server to connect to
    // tran.connect("smtp.qq.com", 25, "xxx@qq.com", "xxxx");//qq-mail
    // server to connect to
    tran.connect("smtp.qq.com", 25, email, emailPassword);
    // Set// mail// recipient (email)  test:2376323219@qq.com  452262448@qq.com  1025807062@qq.com
    tran.sendMessage(messgae, new Address[] { new InternetAddress("1025807062@qq.com") });
    tran.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) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Session(javax.mail.Session)

Example 18 with Transport

use of javax.mail.Transport 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 19 with Transport

use of javax.mail.Transport 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 20 with Transport

use of javax.mail.Transport in project camel by apache.

the class DefaultJavaMailSender method send.

@Override
public void send(MimeMessage mimeMessage) throws MessagingException {
    Transport transport = getTransport(getSession());
    LOG.debug("Connecting to {}:{}", host, port);
    transport.connect(getHost(), getPort(), getUsername(), getPassword());
    try {
        if (mimeMessage.getSentDate() == null) {
            mimeMessage.setSentDate(new Date());
        }
        String messageId = mimeMessage.getMessageID();
        mimeMessage.saveChanges();
        if (messageId != null) {
            // preserve explicitly specified message id, as it may be lost on save
            mimeMessage.setHeader("Message-ID", messageId);
        }
        LOG.debug("Sending MimeMessage: {} using host: {}", mimeMessage, host);
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
    } finally {
        try {
            transport.close();
        } catch (MessagingException e) {
            LOG.warn("Error closing transport to host " + host + ". This exception will be ignored.", e);
        }
    }
}
Also used : MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) Date(java.util.Date)

Aggregations

Transport (javax.mail.Transport)41 Session (javax.mail.Session)28 MimeMessage (javax.mail.internet.MimeMessage)24 JMSession (com.zimbra.cs.util.JMSession)18 MessagingException (javax.mail.MessagingException)18 Test (org.junit.Test)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)14 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)14 InternetAddress (javax.mail.internet.InternetAddress)11 Properties (java.util.Properties)9 Date (java.util.Date)7 Message (javax.mail.Message)6 NoSuchProviderException (javax.mail.NoSuchProviderException)6 SendFailedException (javax.mail.SendFailedException)6 IOException (java.io.IOException)5 SMTPMessage (com.sun.mail.smtp.SMTPMessage)2 SMTPTransport (com.sun.mail.smtp.SMTPTransport)2 Address (javax.mail.Address)2 Multipart (javax.mail.Multipart)2 AddressException (javax.mail.internet.AddressException)2