本文整理汇总了Java中javax.mail.Message.setFrom方法的典型用法代码示例。如果您正苦于以下问题:Java Message.setFrom方法的具体用法?Java Message.setFrom怎么用?Java Message.setFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Message
的用法示例。
在下文中一共展示了Message.setFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendMail
import javax.mail.Message; //导入方法依赖的package包/类
public static void sendMail(String host, int port, String username, String password, String recipients,
String subject, String content, String from) throws AddressException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
message.setSubject(subject);
message.setText(content);
Transport.send(message);
}
示例2: sendTextMail
import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendTextMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
try {
Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
mailMessage.setText(mailInfo.getContent());
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
示例3: sendMsg
import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendMsg(String recipient, String subject, String content)
throws MessagingException {
// Create a mail object
Session session = Session.getInstance(props, new Authenticator() {
// Set the account information session,transport will send mail
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Constants.MAIL_USERNAME, Constants.MAIL_PASSWORD);
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setSubject(subject); //Set the mail subject
msg.setContent(content,"text/html;charset=utf-8");
msg.setFrom(new InternetAddress(Constants.MAIL_USERNAME)); //Set the sender
msg.setRecipient(RecipientType.TO, new InternetAddress(recipient)); //Set the recipient
Transport.send(msg);
return true;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}
}
示例4: sendMail
import javax.mail.Message; //导入方法依赖的package包/类
private void sendMail(String subject, String body, MailSettings settings) throws MessagingException {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", settings.getSmtpServer());
props.put("mail.smtp.port", settings.getSmtpPort() + "");
if (settings.getMode() == MailSettings.Mode.SSL) {
props.put("mail.smtp.socketFactory.port", settings.getSmtpPort() + "");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
} else if (settings.getMode() == MailSettings.Mode.TLS) {
props.put("mail.smtp.starttls.enable", "true");
}
props.put("mail.smtp.auth", settings.isUseAuth() + "");
Session session;
if (settings.isUseAuth()) {
session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(settings.getUser(), settings.getPass());
}
});
} else {
session = Session.getInstance(props);
}
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(settings.getFrom()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(settings.getTo()));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
示例5: sendCode
import javax.mail.Message; //导入方法依赖的package包/类
public static String sendCode(User user, HttpServletRequest request) {
try {
Session session = EmailActions.authorizeWebShopEmail();
String code = Long.toHexString(Double.doubleToLongBits(Math.random()));
request.getSession().setAttribute("deleteAccountCode", code);
request.getSession().setAttribute("userName", user.getLogin());
System.out.println(code);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(user.geteMail(), false));
msg.setSubject("Delete account");
msg.setText("Link : " + ApplicationProperties.URL + ApplicationProperties.PROJECT_NAME + "account/deleteAccountCode/" + code);
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("Error : " + e);
}
return "loginAndRegistration/reset/codePassword";
}
示例6: sendUsername
import javax.mail.Message; //导入方法依赖的package包/类
@RequestMapping(value = "sendUsername", method = RequestMethod.POST)
public String sendUsername(@RequestParam("email") String email, Model model, HttpServletResponse response,
HttpServletRequest request) {
if ((usersService.findByEmail(email) == null)) {
model.addAttribute("msg", "Wrong e-mail");
return "loginAndRegistration/reset/forgotUsername";
}
User user = usersService.findByEmail(email);
try {
Session session = EmailActions.authorizeWebShopEmail();
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
msg.setSubject("Your login");
msg.setText("Login : " + user.getLogin());
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("Error : " + e);
}
model.addAttribute("msg", "Success");
return "loginAndRegistration/reset/forgotUsername";
}
示例7: sendHtmlMail
import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendHtmlMail(MailSenderInfo mailInfo) {
XLog.d("发送网页版邮件!");
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
try {
Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
String[] receivers = mailInfo.getReceivers();
Address[] tos = new InternetAddress[receivers.length];
for (int i = 0; i < receivers.length; i++) {
tos[i] = new InternetAddress(receivers[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, tos);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
示例8: main
import javax.mail.Message; //导入方法依赖的package包/类
public static void main(String[] args) {
Properties props = new Properties();
/** Parâmetros de conexão com servidor Gmail */
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]", "tjm123456");
}
});
/** Ativa Debug para sessão */
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]")); //Remetente
Address[] toUser = InternetAddress //Destinatário(s)
.parse("[email protected]");
message.setRecipients(Message.RecipientType.TO, toUser);
message.setSubject("Enviando email com JavaMail");//Assunto
message.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
/**Método para enviar a mensagem criada*/
Transport.send(message);
System.out.println("Feito!!!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
示例9: sendMail
import javax.mail.Message; //导入方法依赖的package包/类
/**
* 发邮件处理
*
* @param toAddr
* 邮件地址
* @param content
* 邮件内容
* @return 成功标识
*/
public static boolean sendMail(String toAddr, String title, String content, boolean isHtmlFormat) {
final String username = YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_USERNAME);
final String password = YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_PASSWORD);
Properties props = new Properties();
props.put("mail.smtp.auth", YiDuConstants.yiduConf.getBoolean(YiDuConfig.MAIL_SMTP_AUTH, true));
props.put("mail.smtp.starttls.enable",
YiDuConstants.yiduConf.getBoolean(YiDuConfig.MAIL_SMTP_STARTTLS_ENABLE, true));
props.put("mail.smtp.host", YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_HOST));
props.put("mail.smtp.port", YiDuConstants.yiduConf.getInt(YiDuConfig.MAIL_SMTP_PORT, 25));
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(YiDuConstants.yiduConf.getString(YiDuConfig.MAIL_SMTP_FROM)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
message.setSubject(title);
if (isHtmlFormat) {
message.setContent(content, "text/html");
} else {
message.setText(content);
}
Transport.send(message);
} catch (MessagingException e) {
logger.warn(e);
return false;
}
return true;
}
示例10: sendEmail
import javax.mail.Message; //导入方法依赖的package包/类
public static void sendEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message, String nombreArchivoAdj, String urlArchivoAdj)
throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
//Se crea la parte del cuerpo del mensaje.
BodyPart texto = new MimeBodyPart();
texto.setText(message);
BodyPart adjunto = new MimeBodyPart();
if(nombreArchivoAdj != null ){
adjunto.setDataHandler(new DataHandler(new FileDataSource(urlArchivoAdj)));
adjunto.setFileName(nombreArchivoAdj);
}
//Juntar las dos partes
MimeMultipart multiParte = new MimeMultipart();
multiParte.addBodyPart(texto);
if (nombreArchivoAdj != null) multiParte.addBodyPart(adjunto);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = null;
toAddresses = InternetAddress.parse(toAddress, false);
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
//msg.setText(message);
msg.setContent(multiParte);
// sends the e-mail
Transport.send(msg);
}
示例11: sendSMS
import javax.mail.Message; //导入方法依赖的package包/类
public void sendSMS(String smsMessage_arg)
{
props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(getUsername(), getPassword());
}
});
System.out.println("Authentication Complete");
try
{
setSms(smsMessage_arg);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]", "memoranda"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(getAddress(), "smsLine"));
msg.setSubject("Memoranda ");
msg.setText(getSms());
Transport.send(msg);
}
catch(Exception emailErr )
{
System.out.println("error sending email...\n");
emailErr.printStackTrace(System.out);
}
}
示例12: send
import javax.mail.Message; //导入方法依赖的package包/类
boolean send(String title, String body, String toAddr) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(USER_NAME));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddr));
message.setSubject(title);
message.setText(body);
Transport.send(message);
System.out.println("Mail sent.");
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
示例13: sendCode
import javax.mail.Message; //导入方法依赖的package包/类
@RequestMapping(value = "sendCode", method = RequestMethod.POST)
public String sendCode(@RequestParam("login") String login, @RequestParam("email") String email, Model model,
HttpServletResponse response, HttpServletRequest request) {
if ((usersService.findByEmail(email) == null)) {
model.addAttribute("msg", "Wrong e-mail");
return "loginAndRegistration/reset/forgotPassword";
} else if (usersService.findByLogin(login) == null) {
model.addAttribute("msg", "Wrong login");
return "loginAndRegistration/reset/forgotPassword";
}
try {
Session session = EmailActions.authorizeWebShopEmail();
String code = Long.toHexString(Double.doubleToLongBits(Math.random()));
request.getSession().setAttribute("code", code);
request.getSession().setAttribute("email", email);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
msg.setSubject("Reset password");
msg.setText("After 5 min code will be delete\n" + code);
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
System.out.println("Error : " + e);
}
return "loginAndRegistration/reset/codePassword";
}
示例14: sendTextEmail
import javax.mail.Message; //导入方法依赖的package包/类
public static void sendTextEmail(String recvEmail) {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.socketFactory.port", "994");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("463112653", "manllfvunnfwbjhh");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setSubject("Hello Vme");
//整个邮件的MultiPart(不能直接加入内容,需要在bodyPart中加入)
Multipart emailPart = new MimeMultipart();
MimeBodyPart attr1 = new MimeBodyPart();
attr1.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/2601169057.png")));
attr1.setFileName("tip.pic");
MimeBodyPart attr2 = new MimeBodyPart();
attr2.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/1724836491.png")));
attr2.setFileName(MimeUtility.encodeText("哦图像"));
MimeBodyPart content = new MimeBodyPart();
MimeMultipart contentPart = new MimeMultipart();
MimeBodyPart imgPart = new MimeBodyPart();
imgPart.setDataHandler(new DataHandler(new FileDataSource("E:/workspaces/Archon/src/main/webapp/uploadfile/head_img/1724836491.png")));
imgPart.setContentID("pic");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1><a href='www.baidu.com'>百度一下</a><img src='cid:pic'/></h1>", "text/html;charset=utf-8");
contentPart.addBodyPart(imgPart);
contentPart.addBodyPart(htmlPart);
content.setContent(contentPart);
emailPart.addBodyPart(attr1);
emailPart.addBodyPart(attr2);
emailPart.addBodyPart(content);
msg.setContent(emailPart);
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));
msg.setRecipients(RecipientType.CC, InternetAddress.parse("[email protected],[email protected]"));
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: sendMessage
import javax.mail.Message; //导入方法依赖的package包/类
private void sendMessage()
{
try {
String smtpHost="smtp.gmail.com";
String smtpUsername ="[email protected]";
String smtpPassword ="OurAccountEnterprise2015";
String from ="[email protected]";
String to="[email protected]";
String info = "Mensagem enviada de NICON SEGUROS SA por "+feedBack.getNome()+"\n\n"+feedBack.getMensagem()+"\n\n"+feedBack.getEmailResposta();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(feedBack.getAssunto());
message.setText(info);
Transport.send(message);
Mensagem.addInfoMsg("Mensagem enviada com sucesso");
Validacao.atualizar("formEmail", "growlFeedBack");
RequestContext.getCurrentInstance().execute("messageSent()");
}
catch (MessagingException ex)
{
Mensagem.addErrorMsg("Erro a enviar mensagem\n"+ex.getMessage());
Validacao.atualizar("formEmail", "growlFeedBack");
}
}