本文整理汇总了Java中javax.mail.Transport.sendMessage方法的典型用法代码示例。如果您正苦于以下问题:Java Transport.sendMessage方法的具体用法?Java Transport.sendMessage怎么用?Java Transport.sendMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Transport
的用法示例。
在下文中一共展示了Transport.sendMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: completeClientSend
import javax.mail.Transport; //导入方法依赖的package包/类
public void completeClientSend(String mailServer, String... credentials) throws AddressException, MessagingException {
if (credentials != null && credentials.length > 1) {
// Step1
logger.info("\n 1st ===> setup Mail Server Properties..");
logger.info("Mail Server Properties have been setup successfully..");
// Step2
logger.info("\n\n 2nd ===> get Mail .");
getMailSession = getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(TO, new InternetAddress("[email protected]"));
generateMailMessage.addRecipient(CC, new InternetAddress("[email protected]"));
generateMailMessage.setSubject("Greetings from Vige..");
String emailBody = "Test email by Vige.it JavaMail API example. " + "<br><br> Regards, <br>Vige Admin";
generateMailMessage.setContent(emailBody, "text/html");
logger.info("Mail Session has been created successfully..");
// Step3
logger.info("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
// Enter your correct gmail UserID and Password
// if you have 2FA enabled then provide App Specific Password
transport.connect(mailServer, credentials[0], credentials[1]);
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
}
示例2: sendToAdmin
import javax.mail.Transport; //导入方法依赖的package包/类
@Override
public boolean sendToAdmin(final String institutionEmailAddress, final String applicationMessage) {
String[] to = new String[]{"[email protected]"};
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(APPLICATION_SUBJECT);
message.setText(applicationBody + "Email: " + institutionEmailAddress + "\nMessage: " + applicationMessage);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Email sent to Admin");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例3: sendTokenMail
import javax.mail.Transport; //导入方法依赖的package包/类
@Override
public boolean sendTokenMail(final String[] to, final String contractAddress, final String artifactId,
final String registryContract) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(TOKEN_SUBJECT);
message.setText(tokenBody + contractAddress + "\nArtifact ID: " + artifactId
+ "\n\nBelow is the Unilog Registry. Add this into the verifiers portal if you need to"
+ " verify a token: \n" + registryContract);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Token email has been sent");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例4: send
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* @Title: send
* @Description: TODO(发送带附件的邮件)
* @author [email protected] (苟志强)
* @date 2017-6-6 下午4:29:56
* @param to 接收用户账户
* @param title 邮件标题
* @param content 邮件内容
* @param affix 附件路径
* @param affixName 发送后显示名称
* @return 是否发送成功
*/
public static boolean send(String to,String title,String content,String affix,String affixName) {
Properties props = new Properties();
//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
//需要经过授权,也就是有户名和密码的校验,这样才能通过验证
props.put("mail.smtp.auth", "true");
//用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
//有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
//用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true);
//用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
try{
//加载发件人地址
message.setFrom(new InternetAddress(from));
//加载收件人地址
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//加载标题
message.setSubject(title);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
//设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(content);
multipart.addBodyPart(contentPart);
//添加附件
if(affix!=null&&!"".equals(affix)&&affixName!=null&&!"".equals(affixName)){
BodyPart messageBodyPart= new MimeBodyPart();
DataSource source = new FileDataSource(affix);
//添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
//添加附件的标题
messageBodyPart.setFileName(MimeUtility.encodeText(affixName));
multipart.addBodyPart(messageBodyPart);
}
//将multipart对象放到message中
message.setContent(multipart);
//保存邮件
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");
//连接服务器的邮箱
transport.connect(host, user, pwd);
//把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
示例5: sendFeedBackMail
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* 发送反馈邮件
* @param detail
* @throws UnsupportedEncodingException
* @throws MessagingException
*/
public void sendFeedBackMail(String detail) throws UnsupportedEncodingException, MessagingException {
MimeMessage message = createFeedBackMsg(session,myEmailAccount,detail);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例6: sendout
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* 发送邮件
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
logger.info(Resources.getMessage("EMAIL.SENDING"));
Session mailSession = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (userkey == null || "".equals(userkey.trim())) {
return null;
}
return new PasswordAuthentication(username, userkey);
}
});
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
// 设置发送日期
mimeMsg.setSentDate(new Date());
// 发送
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
}
logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
transport.close();
return true;
} catch (Exception e) {
logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
return false;
}
}
示例7: sendout
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* 发送邮件
*
* @param name String
* @param pass String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
logger.info(Resources.getMessage("EMAIL.SENDING"));
Session mailSession = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (userkey == null || "".equals(userkey.trim())) {
return null;
}
return new PasswordAuthentication(username, userkey);
}
});
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
// 设置发送日期
mimeMsg.setSentDate(new Date());
// 发送
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
if (mimeMsg.getRecipients(Message.RecipientType.CC) != null) {
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
}
logger.info(Resources.getMessage("EMAIL.SEND_SUCC"));
transport.close();
return true;
} catch (Exception e) {
logger.error(Resources.getMessage("EMAIL.SEND_ERR"), e);
return false;
}
}
示例8: sendSetupMail
import javax.mail.Transport; //导入方法依赖的package包/类
@Override
public boolean sendSetupMail(final String[] to, final String code, final String subject, final String body) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
if (subject == null || body == null) {
message.setSubject(SETUP_SUBJECT);
message.setText(setupBody + code);
} else {
message.setSubject(subject);
message.setText(body + code);
}
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Setup email has been sent.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例9: sendAlertEmail
import javax.mail.Transport; //导入方法依赖的package包/类
public void sendAlertEmail(String alert) throws MessagingException{
String host = "smtp.gmail.com";
String from = "[email protected]";
String pass = "3inst3in?";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[alert_recipients.length];
// To get the array of addresses
for( int i=0; i < alert_recipients.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(alert_recipients[i]);
}
//System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("WARNING Something went wrong in the Einstein tunnel");
message.setText(alert);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例10: test
import javax.mail.Transport; //导入方法依赖的package包/类
public void test() throws Exception {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
msg.setContent(BODY, "text/plain");
Transport transport = session.getTransport();
try {
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
} finally {
transport.close();
}
}
示例11: sendAccountCreatedMail
import javax.mail.Transport; //导入方法依赖的package包/类
@Override
public boolean sendAccountCreatedMail(final String[] to, final String email) {
setupProperties();
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(username));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(DETAILS_SUBJECT);
message.setText(detailsBody + "username: " + email);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
LOGGER.info("Login details email has been sent.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例12: send
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* 发送邮件
*
* @param email-收件人
* @throws Exception
* @return new password
*/
public static String send(String email, String username) throws Exception {
String pwd = getRandomString(6);
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件
MimeMessage message = createMimeMessage(session, myEmailAccount, email, username,pwd);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
//
// PS_01: 成败的判断关键在此一句, 如果连接服务器失败, 都会在控制台输出相应失败原因的 log,
// 仔细查看失败原因, 有些邮箱服务器会返回错误码或查看错误类型的链接, 根据给出的错误
// 类型到对应邮件服务器的帮助网站上查看具体失败原因。
//
// PS_02: 连接失败的原因通常为以下几点, 仔细检查代码:
// (1) 邮箱没有开启 SMTP 服务;
// (2) 邮箱密码错误, 例如某些邮箱开启了独立密码;
// (3) 邮箱服务器要求必须要使用 SSL 安全连接;
// (4) 请求过于频繁或其他原因, 被邮件服务器拒绝服务;
// (5) 如果以上几点都确定无误, 到邮件服务器网站查找帮助。
//
// PS_03: 仔细看log, 认真看log, 看懂log, 错误原因都在log已说明。
transport.connect(myEmailAccount, myEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人,
// 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
return pwd;
}
示例13: sendEmail
import javax.mail.Transport; //导入方法依赖的package包/类
/**
* Versendet die EMail.
*
* @throws MessagingException -
*/
public void sendEmail() throws MessagingException {
Transport transport = mailSession.getTransport("smtp");
String emailHost = "smtp.gmail.com";
transport.connect(emailHost, fromEmail, fromPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
}
示例14: send
import javax.mail.Transport; //导入方法依赖的package包/类
public void send ( final String subject, final String text ) throws MessagingException
{
final Message message = new MimeMessage ( this.session );
if ( this.from != null )
{
message.setFrom ( this.from );
}
else
{
message.setFrom ();
}
message.setHeader ( "Return-Path", "<>" );
message.setRecipients ( javax.mail.Message.RecipientType.TO, this.to );
message.setSubject ( subject );
message.setText ( text );
message.saveChanges ();
final Transport transport = this.session.getTransport ();
logger.debug ( "Connecting transport..." );
transport.connect ();
logger.debug ( "Connecting transport... done!" );
try
{
logger.debug ( "Sending message..." );
transport.sendMessage ( message, message.getAllRecipients () );
logger.debug ( "Sending message... done!" );
}
finally
{
logger.debug ( "Closing transport..." );
try
{
transport.close ();
logger.debug ( "Closing transport... done!" );
}
catch ( final Exception e )
{
logger.info ( "Failed to close transport", e );
}
}
}
示例15: sendMail
import javax.mail.Transport; //导入方法依赖的package包/类
public boolean sendMail(String to,String otp) throws MessagingException
{
String host="smtp.gmail.com";
String username="";//emailid
String password="";//emailid password
String from=" ";// email from which u have to send
String subject="One Time Password";
String body="Your One Time passsword is "+otp;
boolean sessionDebug=false;
Properties props=System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
Session mailSession=Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg=new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport tr=mailSession.getTransport("smtp");
tr.connect(host,username,password);
msg.saveChanges();
tr.sendMessage(msg,msg.getAllRecipients());
tr.close();
//Transport.send(msg);
return true;
}