本文整理汇总了Java中com.sun.mail.smtp.SMTPTransport.close方法的典型用法代码示例。如果您正苦于以下问题:Java SMTPTransport.close方法的具体用法?Java SMTPTransport.close怎么用?Java SMTPTransport.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.mail.smtp.SMTPTransport
的用法示例。
在下文中一共展示了SMTPTransport.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
if (StringUtils.isBlank(senderAddress)) {
throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " +
"You must set a value for notifications to work");
}
if (StringUtils.isBlank(content)) {
throw new InternalServerError("Notification content is empty");
}
try {
InternetAddress sender = new InternetAddress(senderAddress, senderName);
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
msg.setHeader("X-Mailer", "mxisd"); // FIXME set version
msg.setSentDate(new Date());
msg.setFrom(sender);
msg.setRecipients(Message.RecipientType.TO, recipient);
msg.saveChanges();
log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(cfg.getTls() > 0);
transport.setRequireStartTLS(cfg.getTls() > 1);
log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
try {
transport.sendMessage(msg, InternetAddress.parse(recipient));
log.info("Invite to {} was sent", recipient);
} finally {
transport.close();
}
} catch (UnsupportedEncodingException | MessagingException e) {
throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
}
}
示例2: sendEmail
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public void sendEmail() throws MessagingException {
checkSettings();
Properties props = new Properties();
if (_usesAuth) {
props.put("mail." + protocol + ".auth", "true");
props.put("mail.user", _mailUser);
props.put("mail.password", _mailPassword);
} else {
props.put("mail." + protocol + ".auth", "false");
}
props.put("mail." + protocol + ".host", _mailHost);
props.put("mail." + protocol + ".timeout", _mailTimeout);
props.put("mail." + protocol + ".connectiontimeout", _connectionTimeout);
props.put("mail.smtp.starttls.enable", _tls);
props.put("mail.smtp.ssl.trust", _mailHost);
Session session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
InternetAddress from = new InternetAddress(_fromAddress, false);
message.setFrom(from);
for (String toAddr : _toAddress)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
toAddr, false));
message.setSubject(_subject);
message.setSentDate(new Date());
if (_attachments.size() > 0) {
MimeMultipart multipart =
this._enableAttachementEmbedment ? new MimeMultipart("related")
: new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(_body.toString(), _mimeType);
multipart.addBodyPart(messageBodyPart);
// Add attachments
for (BodyPart part : _attachments) {
multipart.addBodyPart(part);
}
message.setContent(multipart);
} else {
message.setContent(_body.toString(), _mimeType);
}
// Transport transport = session.getTransport();
SMTPTransport t = (SMTPTransport) session.getTransport(protocol);
try {
connectToSMTPServer(t);
} catch (MessagingException ste) {
if (ste.getCause() instanceof SocketTimeoutException) {
try {
// retry on SocketTimeoutException
connectToSMTPServer(t);
logger.info("Email retry on SocketTimeoutException succeeded");
} catch (MessagingException me) {
logger.error("Email retry on SocketTimeoutException failed", me);
throw me;
}
} else {
logger.error("Encountered issue while connecting to email server", ste);
throw ste;
}
}
t.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
t.close();
}
示例3: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void send(String to, String sub, String msg) {
Properties prop = System.getProperties();
prop.put("mail.smtps.host", host);
prop.put("mail.smtps.auth", "true");
Session session = Session.getInstance(prop);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect(host, user, password);
t.sendMessage(message, message.getAllRecipients());
t.close();
System.out.println("Email sent to "+to);
} catch (MessagingException ex) {
Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: mailSenderEmpty
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
@Test
public void mailSenderEmpty() throws IOException, MessagingException {
Socket smtpSocket;
String hostAddress = greenMail.getSmtp().getBindTo();
int port = greenMail.getSmtp().getPort();
Session smtpSession = greenMail.getSmtp().createSession();
URLName smtpURL = new URLName(hostAddress);
SMTPTransport smtpTransport = new SMTPTransport(smtpSession, smtpURL);
try {
smtpSocket = new Socket(hostAddress, port);
smtpTransport.connect(smtpSocket);
assertThat(smtpTransport.isConnected(),is(equalTo(true)));
smtpTransport.issueCommand("MAIL FROM: <>", -1);
assertThat("250 OK", equalToIgnoringWhiteSpace(smtpTransport.getLastServerResponse()));
} finally {
smtpTransport.close();
}
}
示例5: sendMail
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void sendMail(String to, String subject, String content) throws MessagingException
{
String server_email = TheConfig.getProperty(TheConfig.SYSTEMEMAIL_ADDRESS);
String server_pwd = TheConfig.getProperty(TheConfig.SYSTEMEMAIL_PASSWORD);
Properties props = System.getProperties();
props.put("mail.smtps.host",TheConfig.getProperty("mail.smtps.host"));
props.put("mail.smtps.auth","true");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(server_email));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(content);
msg.setHeader("X-Mailer", "Sisob Data Extractor System");
msg.setSentDate(new Date());
SMTPTransport t =(SMTPTransport)session.getTransport("smtps");
t.connect(TheConfig.getProperty("mail.smtps.host"), server_email, server_pwd);
t.sendMessage(msg, msg.getAllRecipients());
LOG.info("Email Response To " + to + ": " + t.getLastServerResponse());
t.close();
}
示例6: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static void send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message, MailConsts mailConsts) throws AddressException, MessagingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", mailConsts.getHost());
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", String.valueOf(mailConsts.getPort()));
props.setProperty("mail.smtp.socketFactory.port", String.valueOf(mailConsts.getPort()));
props.setProperty("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
final MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username + "@" + mailConsts.getPostfix()));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect(mailConsts.getHost(), username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
示例7: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private void send(MimeMessage msg) throws MessagingException {
msg.setHeader("X-Mailer", "matrix-appservice-email");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, getIdentity());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(cfg.getTls() > 0);
transport.setRequireStartTLS(cfg.getTls() > 1);
transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
log.info("Sending email via SMTP using {}:{}", cfg.getHost(), cfg.getPort());
try {
transport.sendMessage(msg, InternetAddress.parse(getIdentity()));
} catch (MessagingException e) {
log.error("mmm", e);
} finally {
transport.close();
}
}
示例8: sendEmail
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public void sendEmail() throws MessagingException {
checkSettings();
Properties props = new Properties();
// props.setProperty("mail.transport.protocol", "smtp");
props.put("mail."+protocol+".host", _mailHost);
props.put("mail."+protocol+".auth", "true");
props.put("mail.user", _mailUser);
props.put("mail.password", _mailPassword);
props.put("mail."+protocol+".timeout", _mailTimeout);
props.put("mail."+protocol+".connectiontimeout", _connectionTimeout);
Session session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
InternetAddress from = new InternetAddress(_fromAddress, false);
message.setFrom(from);
for (String toAddr : _toAddress)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
toAddr, false));
message.setSubject(_subject);
message.setSentDate(new Date());
if (_attachments.size() > 0) {
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(_body.toString(), _mimeType);
multipart.addBodyPart(messageBodyPart);
// Add attachments
for (BodyPart part : _attachments) {
multipart.addBodyPart(part);
}
message.setContent(multipart);
} else {
message.setContent(_body.toString(), _mimeType);
}
// Transport transport = session.getTransport();
SMTPTransport t = (SMTPTransport) session.getTransport(protocol);
t.connect(_mailHost, _mailUser, _mailPassword);
t.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
t.close();
}
示例9: sendMessage
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public void sendMessage(final MimeMessage msg) throws MessagingException {
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress("Neutrinet <" + user + '>'));
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect(VPN.cfg.getProperty("smtp.server"), user, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
示例10: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
private void send(Message msg) throws MessagingException
{
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
try
{
t.connect(this.getHost(), this.getFrom(), this.getPassword());
t.sendMessage(msg, msg.getAllRecipients());
} finally
{
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
示例11: sendMail
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public synchronized void sendMail(String subject, String body, String user, String recipients) {
if (!Utils.isNetworkConnected(context, LOGS)) {
LOGS.info("GMailOAuth2Sender. No network.");
return;
}
SMTPTransport smtpTransport = null;
try {
LOGS.info("GMailOAuth2Sender. Connecting to SMTP");
smtpTransport = connectToSmtp("smtp.gmail.com",
587,
user,
token,
true);
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(user));
message.setSubject(subject);
message.setContent(body, "text/html; charset=utf-8");
try {
LOGS.info("GMailOAuth2Sender. Setting email recipients");
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(recipients));
LOGS.info("GMailOAuth2Sender. Sending email. Recipients: " + recipients);
smtpTransport.sendMessage(message, message.getAllRecipients());
} finally {
smtpTransport.close();
}
} catch (MessagingException e) {
LOGS.error("Can't send mail", e);
mAccountManager.invalidateAuthToken("com.google", token);
initToken();
sendMail(subject, body, user, recipients);
}
}
示例12: Send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
/**
* Send email using GMail SMTP server.
*
* @param username GMail username
* @param password GMail password
* @param recipientEmail TO recipient
* @param ccEmail CC recipient. Can be empty if there is no CC recipient
* @param title title of the message
* @param message message to be sent
* @throws AddressException if the email address parse failed
* @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
*/
private static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException, EmailInesistente {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtps.auth", "true");
/*
If set to false, the QUIT command is sent and the connection is immediately closed. If set
to true (the default), causes the transport to wait for the response to the QUIT command.
ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
http://forum.java.sun.com/thread.jspa?threadID=5205249
smtpsend.java - demo program from javamail
*/
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@gmail.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("smtp.gmail.com", username, password);
try{
t.sendMessage(msg, msg.getAllRecipients());
}
catch (SendFailedException e){
throw new EmailInesistente();
}
t.close();
}
示例13: sendMessage
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
public static boolean sendMessage(final String toAddress, final String fromAddress, final String subject, final String message) {
log.debug("Sending email to "+toAddress);
// Sender's email ID needs used with authentication
final String username = "anEmailUserGoesHere*****";
// Assuming you are sending email from host
final String host = "emailhost";
final String password = "*************";
java.security.Security.setProperty("ssl.SocketFactory.provider", "sun.security.ssl.SSLSocketFactoryImpl");
java.security.Security.setProperty("ssl.ServerSocketFactory.provider", "sun.security.ssl.SSLSocketFactoryImpl");
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", host);
props.setProperty("mail.smtp.port", "465"); // SSL
props.setProperty("mail.smtp.socketFactory.port", "465"); // SSL
//props.setProperty("mail.smtp.port", "587"); // non ssl
//props.setProperty("mail.smtp.socketFactory.port", "587"); // non ssl
props.setProperty("mail.smtps.auth", "true"); // SSL
// props.setProperty("mail.smtp.auth", "true"); // no ssl
props.put("mail.smtps.quitwait", "false"); // SSL
//props.put("mail.smtp.quitwait", "false");
log.debug("Creating auth session");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false));
msg.setSubject(subject);
msg.setContent(message,"text/html");
msg.setSentDate(new Date());
log.info("Sending smtp msg");
SMTPTransport t = (SMTPTransport)session.getTransport("smtps"); // SSL
//SMTPTransport t = (SMTPTransport)session.getTransport("smtp"); // no ssl
log.info("got smtp transport");
t.connect(host, username, password);
log.info("connected smtp");
t.sendMessage(msg, msg.getAllRecipients());
log.info("sent smtp");
t.close();
}
catch (MessagingException e) {
log.error("Failed to send email to " + toAddress, e);
e.printStackTrace();
return false;
}
log.info("Completed email send to " + toAddress + " successfully.");
return true;
}
示例14: Send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
/**
* Send email using GMail SMTP server.
*
* @param username GMail username
* @param password GMail password
* @param recipientEmail TO recipient
* @param ccEmail CC recipient. Can be empty if there is no CC recipient
* @param title title of the message
* @param message message to be sent
* @throws AddressException if the email address parse failed
* @throws MessagingException if the connection is dead or not in the connected state or if the
* message is not a MimeMessage
*/
public static void Send(final String username, final String password, String recipientEmail,
String ccEmail, String title, String message) throws AddressException, MessagingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtps.auth", "true");
/*
* If set to false, the QUIT command is sent and the connection is immediately closed. If set to
* true (the default), causes the transport to wait for the response to the QUIT command. ref :
* http://java .sun.com/products/javamail/javadocs/com/sun/mail/smtp/package -summary.html
* http://forum.java.sun.com/thread.jspa?threadID=5205249 smtpsend.java - demo program from
* javamail
*/
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
// -- Create a new message --
final MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@gmail.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect("smtp.gmail.com", username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
示例15: send
import com.sun.mail.smtp.SMTPTransport; //导入方法依赖的package包/类
@Override
public synchronized void send(String subject, String body, String senderAddress,
String toAddress, String oauthToken) throws NotificationFailureException {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
props.put("mail.smtp.ssl.enable", true);
Session session = Session.getInstance(props);
session.setDebug(true);
SMTPTransport smtpTransport = null;
try {
smtpTransport = connectToSmtp(session, "smtp.gmail.com",
587,
senderAddress,
oauthToken);
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(senderAddress));
message.setSubject(subject);
message.setContent(body, "text/html; charset=utf-8");
if(toAddress.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
smtpTransport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException mex) {
throw new NotificationFailureException("Failed to send Gmail email from: " + senderAddress, mex);
}
finally {
if(smtpTransport !=null){
try {
smtpTransport.close();
} catch (MessagingException e) {
throw new NotificationFailureException("Failed to send Gmail email from: " + senderAddress, e);
}
}
}
}