本文整理汇总了Java中javax.mail.internet.MimeMessage.setFrom方法的典型用法代码示例。如果您正苦于以下问题:Java MimeMessage.setFrom方法的具体用法?Java MimeMessage.setFrom怎么用?Java MimeMessage.setFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.internet.MimeMessage
的用法示例。
在下文中一共展示了MimeMessage.setFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCodeMessage
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
/**
* 创建一封邮件
*
* @param session 和服务器交互的会话
* @param sendMail 发件人邮箱
* @param receiveMail 收件人邮箱
* @return
* @throws Exception
*/
private MimeMessage createCodeMessage(Session session, String sendMail, String receiveMail) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sendMail, "吃在华科", "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, this.name, "UTF-8"));
message.setSubject("吃在华科邮件注册验证码", "UTF-8");
String content = this.name + ",你好, 您的验证码如下<br/>" + getCode() + "<p> 您不需要回复这封邮件。<p/>";
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
示例2: sendMailBatch
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public void sendMailBatch(List<String> to, String title, String content) throws MessagingException {
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
to.forEach(address -> {
try {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
} catch (MessagingException ignored) {
}
});
message.setSubject(title);
message.setContent(content, "text/html;charset=UTF-8");
Transport.send(message);
}
示例3: sendTokenMail
import javax.mail.internet.MimeMessage; //导入方法依赖的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.internet.MimeMessage; //导入方法依赖的package包/类
/**
* 发送邮件
* @param receiver 接收者邮箱地址
* @param subject 邮件主题
* @param content 邮件内容
*/
public static void send(String receiver, String subject, String content) {
try {
//设置SSL连接、邮件环境
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.smtp.host", SMTP_HOST);
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", SMTP_PORT);
props.setProperty("mail.smtp.socketFactory.port", SMTP_SOCKET_FACTORY_PORT);
props.setProperty("mail.smtp.auth", "true");
//建立邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator() {
//身份认证
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SENDER, PASSWORD);
}
});
//建立邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(SENDER, SENDER_NAME));
message.setRecipients(Message.RecipientType.TO, receiver);
message.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(content, "text/html;charset=UTF-8");
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(mimeBodyPart);
message.setContent(mimeMultipart);
message.saveChanges();
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: createEmailMessage
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
/**
* Bereitet den Mailinhalt zum Senden vor.
*
* @throws MessagingException -
* @throws UnsupportedEncodingException -
*/
public void createEmailMessage() throws MessagingException, UnsupportedEncodingException {
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
for (String toEmail : toEmailList) {
emailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
}
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");
}
示例6: createEmail
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public static MimeMessage createEmail(String to, String from, String subject,
String bodyText) throws MessagingException{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(from);
email.setFrom(fAddress);
email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress);
email.setSubject(subject);
email.setText(bodyText);
return email;
}
示例7: sendEmail
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public static void sendEmail(EmailSettingContent emailSetting, String acceptor, String subject, String body) {
Properties props = buildProperty(emailSetting);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = null;
String password = null;
if (emailSetting.isAuthenticated()) {
username = emailSetting.getUsername();
password = emailSetting.getPassword();
}
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailSetting.getSender()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(acceptor));
message.setSubject(subject, "utf8");
message.setContent(body, "text/html;charset=utf8");
Transport.send(message);
} catch (Throwable throwable) {
}
}
示例8: test
import javax.mail.internet.MimeMessage; //导入方法依赖的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();
}
}
示例9: enviarMail
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public static void enviarMail(Persona cliente, String asunto, String mensaje) {
if (cliente.getPersonaMail() != null) {
Mail mail = obtenerMailEmisor();
Properties props = conectarse(mail);
Session session = autentificar(mail, props);
try {
BodyPart texto = new MimeBodyPart();
// Texto del mensaje
texto.setText(mensaje);
MimeMultipart multiParte = new MimeMultipart();
multiParte.addBodyPart(texto);
MimeMessage message = new MimeMessage(session);
// Se rellena el From
InternetAddress emisor = new InternetAddress(mail.getNombre() + " <" + mail.getMail() + ">");
message.setFrom(emisor);
// Se rellenan los destinatarios
InternetAddress receptor = new InternetAddress();
receptor.setAddress(cliente.getPersonaMail());
message.addRecipient(Message.RecipientType.TO, receptor);
// Se rellena el subject
message.setSubject(asunto);
// Se mete el texto y la foto adjunta.
message.setContent(multiParte);
Transport.send(message);
} catch (MessagingException e) {
messageService.informUser("Poliza creada, falló envío de mail");
}
}
}
示例10: sendMultipartMessage
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public void sendMultipartMessage(String subject, String[] to, String text, String attach)
throws MessagingException, IOException {
MimeMessage message = new MimeMessage(senderSession);
message.setFrom(new InternetAddress(pManager.get_SENDER_From())); // FROM
for(int i=0; i < to.length; i++) {
if(!to[i].equals("")) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); // TO
}
}
message.setSubject(subject); //SUBJECT
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(text);
mp.addBodyPart(textPart); // TEXT
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(attach);
mp.addBodyPart(attachPart); // ATTACH
message.setContent(mp);
transport.sendMessage(message, message.getAllRecipients());
}
示例11: createAttachMail
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
/**
* @Method: createAttachMail
* @Description: ����һ����������ʼ�
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(String subject, String content, String filePath) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
if(recipients.contains(";")){
List<InternetAddress> list = new ArrayList<InternetAddress>();
String []median=recipients.split(";");
for(int i=0;i<median.length;i++){
list.add(new InternetAddress(median[i]));
}
InternetAddress[] address =list.toArray(new InternetAddress[list.size()]);
message.setRecipients(Message.RecipientType.TO,address);
}else{
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
}
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(filePath));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
return message;
}
示例12: send
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public boolean send() throws Exception {
Properties props = _setProperties();
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[] {
new InternetAddress("[email protected]")
});
msg.setSentDate(new Date());
msg.setContent(multipart);
Transport.send(msg);
return true;
}
示例13: sendMail
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public void sendMail(String account) throws Exception {
message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(account, false));
message.setSubject("DO NOT REPLY");
message.setDataHandler(new DataHandler(new ByteArrayDataSource(getMailContent(), "text/html")));
message.setSentDate(new Date());
transport = session.getTransport();
transport.connect("smtp-mail.outlook.com", "[email protected]", "BaoZJ960606");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
示例14: send
import javax.mail.internet.MimeMessage; //导入方法依赖的package包/类
public void send(String to, String cc, String bcc, String subject,
String content, JavamailConfig javamailConfig)
throws MessagingException {
logger.debug("send : {}, {}", to, subject);
try {
Properties props = createSmtpProperties(javamailConfig);
String username = javamailConfig.getUsername();
String password = javamailConfig.getPassword();
// 创建Session实例对象
Session session = Session.getInstance(props, new SmtpAuthenticator(
username, password));
session.setDebug(false);
// 创建MimeMessage实例对象
MimeMessage message = new MimeMessage(session);
// 设置邮件主题
message.setSubject(subject);
// 设置发送人
message.setFrom(new InternetAddress(username));
// 设置发送时间
message.setSentDate(new Date());
// 设置收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
// 设置html内容为邮件正文,指定MIME类型为text/html类型,并指定字符编码为gbk
message.setContent(content, "text/html;charset=gbk");
// 保存并生成最终的邮件内容
message.saveChanges();
// 发送邮件
Transport.send(message);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
示例15: Send
import javax.mail.internet.MimeMessage; //导入方法依赖的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();
}