For a project i need to send a email with html content, but i didn't find a way with pb. However i knew do it with java, it's the raison as i've created this module it use a java file jar you have to a virtual java machine instaled on the pc for use this module.
You can do those things with it:
- Created one or some messages.
- Choosing one target address or some addresses
- Add one ou some attachments.
You can use only JMail.jar with yourself code, for do it you have to create a file into the jar directory named args.txt like ascii with the datas like this
- from eamil address
- smtp host
- smtp port
- ASYNC or SSL
- smtp user
- smtp password
- subject
- html message
- Empty or with the attachments list separated by @
You can get the result with the file status.log
- sended -> the message was sent
- failure -> the message wasn't sent
for example :
Code: Select all
XIncludeFile "../EMAIL_HTLM/EMAIL.pbi"
; WARNING GET THE DIRECTORY OF JAR JMail.jar
; FOR EXAMPLE
EMAIL::JARDirectory = "A:\PB_HTML_EMAIL\EMAIL_HTLM\JAR\"
; WARNING FILL THIS CONSTANTES
#SMTP_HOST = "pro.eu.turbo-smtp.com" ; for exmaple with turbo smtp
#SMTP_PORT = 587
#SMTP_USER = "yourAddress@hotmail.com"
#SMTP_PSW = "yourPsw"
#FROM = "addressToFrom@gmail.com"
Global mail.EMAIL::Email = EMAIL::newEmail(#SMTP_HOST,#SMTP_PORT,#SMTP_USER,#SMTP_PSW)
msg.s = "<h1> Ceci est un teste </h1><br>"+
"<p>Le <b>Lorem Ipsum</b> est simplement du faux texte employé dans la "+
"composition et la mise en page avant impression. Le Lorem Ipsum est le faux "+
"texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme"+
" assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte."+
" Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique,"+
" sans que son contenu n'en soit modifié. Il a été popularisé dans les années 1960 grâce à la vente "+
"de feuilles Letraset contenant des passages du Lorem Ipsum, et, plus récemment, par son inclusion "+
"dans des applications de mise en page de texte, comme Aldus PageMaker.</p>"
; WARNING GET A CORRECT EMAIL ADRESS
Define email_1.s = "targetEmail@gmail.com"
Define email_2.s = "targetEmail@hotmail.com"
Define m.EMAIL::Message = mail\addMessage(EMAIL::newMessage(#FROM,email_1,"msg 1",msg))
m\addTargetAddress(email_2)
Define m.EMAIL::Message = mail\addMessage(EMAIL::newMessage(#FROM,email_1,"msg 2",msg))
m\addTargetAddress(email_2)
mail\send()
; TEST IF MESSAGES ARE BE SENDED
mail\resetMessage()
While mail\nextMessage()
Define ms.EMAIL::Message = mail\getMessage()
ms\resetTargetAddress()
While ms\nextTargetAddress()
If ms\isSended()
Debug "message to "+ms\getEmailAddress()+" has was sended"
Else
Debug "message to "+ms\getEmailAddress()+" diden't sended"
EndIf
Wend
Wend
Code: Select all
package jmail;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
/**
*
* @author microdev
*/
public class JMail {
private static String from;
private static String smtpHost;
private static String smtpPort;
private static String smtpType;
private static String smtpLogin;
private static String smtpPsw;
private static String msgSubject;
private static String msgContent;
private static String msgTarget;
private static String[] myAttachements;
private static boolean isAtachement;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if(readArguments()){
sendEmail();
}else{
writeStatus("failure");
}
}
private static void sendEmail(){
ArrayList<MimeBodyPart> attachements = new ArrayList<>();
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
if ( smtpType == "SSL") {
properties.put("mail.smtp.starttls.enable", "true");
}
properties.put("mail.smtp.host",smtpHost);
properties.put("mail.smtp.port",smtpPort);
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpLogin,smtpPsw);
}
});
try {
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(msgTarget));
message.setSubject(msgSubject);
// create body part for the message
MimeBodyPart messaBodyPart = new MimeBodyPart();
messaBodyPart.setContent(msgContent, "text/html;charset=UTF-8");
if(isAtachement){
for(String a : myAttachements){
attachements.add(new MimeBodyPart());
DataSource ds = new FileDataSource(a);
attachements.get(attachements.size()-1).setDataHandler(new DataHandler(ds));
attachements.get(attachements.size()-1).setFileName(new File(a).getName());
multipart.addBodyPart(attachements.get(attachements.size()-1));
}
}
multipart.addBodyPart(messaBodyPart);
message.setContent(multipart,"UTF-8");
Transport.send(message);
writeStatus("sended");
} catch (MessagingException e) {
e.printStackTrace();
writeStatus("failure");
}
}
private static boolean readArguments() {
boolean valRet = true;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("args.txt"));
if (reader != null) {
// 1 from
String line = reader.readLine();
if (line != null) {
from = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 2
line = reader.readLine();
if (line != null) {
smtpHost = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 3
line = reader.readLine();
if (line != null) {
smtpPort = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 4
line = reader.readLine();
if (line != null) {
smtpType = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 5
line = reader.readLine();
if (line != null) {
smtpLogin = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 6
line = reader.readLine();
if (line != null) {
smtpPsw = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 6
line = reader.readLine();
if (line != null) {
msgSubject = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 7
line = reader.readLine();
if (line != null) {
msgContent = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 8
line = reader.readLine();
if (line != null) {
msgTarget = new String(line.getBytes("ISO-8859-1"));
} else {
valRet = false;
}
// 9
line = reader.readLine();
if (line != null) {
myAttachements = line.split("@");
isAtachement = true;
} else {
isAtachement = false;
}
} else {
valRet = false;
}
} catch (IOException ex) {
valRet = false;
}
return valRet;
}
private static boolean writeStatus(String status) {
boolean valRet = true;
try {
FileWriter fw;
fw = new FileWriter("status.log");
if (fw != null) {
PrintWriter st = new PrintWriter(fw);
st.println(status);
fw.close();
}
} catch (IOException ex) {
valRet = false;
}
return valRet;
}
}