Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

Thursday, April 21, 2022

Sending mails/ sms from Linux server

You have to configure mail server in Linux server. Then you can use following command to send mails.

For mail -

mail -s "subject" recipients_emails < mail body

For SMS -

mail -s "subject" number@your_email_domain < mail body


Monday, January 30, 2012

Embed Image in Email Using java

Following code snippet can be used to send email with image in the HTML body.

Note :- HTML body must be contain image tag as follows.

<img height="55" src="cid:image-id" width="65" />

Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp host ip ");

Session session = Session.getDefaultInstance(properties, null);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());



MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));

Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);



MimeBodyPart imagePart = new MimeBodyPart();
DataSource fds = new FileDataSource("image path ");
imagePart.setDataHandler(new DataHandler(fds));
imagePart.setFileName("logo.jpg");
imagePart.setHeader("Content-ID", "<
image-id>");
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);



message.setContent(multipart);
Transport.send(message);

Monday, December 14, 2009

Graphical + Attachment java Email Client

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailClient {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_AUTH_USER =
xxx@gmail.com;
private static final String SMTP_AUTH_PWD = "xxx";


public static void main(String[] args) throws Exception {

new SimpleMailWithAttachment().test();

}

public void test() throws Exception {
Properties props = new Properties();

props.put("mail.transport.protocol", "smtp");
props.put("mail.host", SMTP_HOST_NAME);

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail with attachment");
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "text/html");
MimeBodyPart attachFilePart = new MimeBodyPart();

FileDataSource fds =
new FileDataSource("d:\\TwainExample.java");

attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
message.setContent(mp);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("
xxx@yahoo.com"));
transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER,
SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}