1
我一直在尋找一種方法來發送電子郵件從我的zohomail帳戶使用JAVA郵件api,我已經通過網上很多例子,但他們都沒有工作。總是有設置屬性的問題。通過zohomail的論壇後,我發現下面的代碼適用於我。使用SSL從zohomail賬戶發送郵件
我一直在尋找一種方法來發送電子郵件從我的zohomail帳戶使用JAVA郵件api,我已經通過網上很多例子,但他們都沒有工作。總是有設置屬性的問題。通過zohomail的論壇後,我發現下面的代碼適用於我。使用SSL從zohomail賬戶發送郵件
下面是一個JAVA程序,用於通過在zohomail上註冊的電子郵件ID發送電子郵件。該程序使用java郵件API。
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailTest
{ public static void main(String[] args)
{ Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "smtp.zoho.com");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.port", "465");
properties.setProperty("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.debug", "true");
properties.put("mail.store.protocol", "pop3");
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.debug.auth", "true");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(properties,new javax.mail.Authenticator()
{ @Override
protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("[email protected]","passwordofid");
}
});
try
{ MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse("[email protected]"));
message.setSubject("Test Subject");
message.setText("Test Email Body");
Transport.send(message);
}
catch (MessagingException e)
{ e.printStackTrace();
}
}
}