Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Coding error..?

Featured Replies

Hello folks

I am relatively new to groovy or JavaScript and therefore I got stuck with a situation in a coding and need some advice or help: The script shown below works quite fine (eMail-sending with multiple mail addresses and attachments).

In the middle, there's a section starting with the comment "//add multipart...". There I would like to set the correct mail multipart type, but somehow the plugin does not accept this and the if-statement is not even entered.

Could anybody "enlighten" me what error there must be in my code?

Thanks a lot for your appreciated help!

Martin

---Parameters---

{from= Newsletter|[email protected], to=Martin Brunner|[email protected], cc=null, bcc=null, subject=Testmail mit Attachments, plainText=Das wäre der reine Text mit ä und ö und ü oder auch Ä Ö und Ü - damit sich sehen kann, ob das auch korrekt durchkommt., htmlText=
I'm testing the Sending Email HTML-Formatted utility for ScriptMaster. Works great!


, attachments=scs1.png|/Users/Dekstop/scs1.png, smtpHost=mail.domain.com, portNumber=25, username=Username, password=123456, priority=3, delimitor=|}



---Script---

RegisterGroovy( "SendEMMultiPartAuthattachments( from ; to ; cc ; bcc ; subject ; plainText ; htmlText ; attachments ; smtpHost ; portNumber ; username ; password ; priority ; delimitor )" ; "

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

import java.io.*;



// set properties

Properties props = new Properties();

props.setProperty("mail.smtp.host", smtpHost);

if(username!=null && password!=null) {

	props.setProperty("mail.smtp.auth", "true");

	props.setProperty("mail.smtps.auth", "true");

}



// set session and message

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

Message message = new MimeMessage(session);



// set hheaders

if(priority!=null) {

	message.addHeader("X-Priority", priority);

} else {

	message.addHeader("X-Priority", "3");

}

message.addHeader("X-Mailer", "FM-Mailer 1.0");

message.addHeader("Mime-Version", "1.0");

message.setSentDate(new Date());

message.setSubject(subject);



// set from and reply to

String fromline = (from);

String fromdelims = "["+ (delimitor) +"]";

String[] FromParts = fromline.split(fromdelims);

String fromName = FromParts [0];

String fromAddress = FromParts [1];

message.setFrom(new InternetAddress(fromAddress, fromName));

message.setReplyTo(new InternetAddress(fromAddress, fromName));



//------------------------------------------------------------------------

// set to addresses

String mailAddressTo=(to);

String[] ToAddresses = mailAddressTo.split("n");

InternetAddress[] mailAddress_TO = new InternetAddress [ToAddresses.length] ;

for (int i=0; i

	String toline = ToAddresses[i];

	String todelims = "["+ (delimitor) +"]";

	String[] ToParts = toline.split(todelims);

	String ToName = ToParts [0];

	String ToAddress = ToParts [1];

mailAddress_TO[i] = new InternetAddress(ToAddress, ToName);

}

message.addRecipients(Message.RecipientType.TO, mailAddress_TO);



// set cc addresses

if(cc!=null) {

	String mailAddressCC=(cc);

	String[] CCAddresses = mailAddressCC.split("n") ;

	InternetAddress[] mailAddress_CC = new InternetAddress [CCAddresses.length] ;

	for (int i=0; i

		String ccline = CCAddresses[i];

		String ccdelims = "["+ (delimitor) +"]"; 

		String[] CCParts = ccline.split(ccdelims);

		String CCName = CCParts [0];

		String CCAddress = CCParts [1];

	mailAddress_CC[i] = new InternetAddress(CCAddress, CCName);

	}

message.addRecipients(Message.RecipientType.CC, mailAddress_CC);

}



// set bcc adresses

if(bcc!=null) {

	String mailAddressBCC=(bcc); 

	String[] BCCAddresses = mailAddressBCC.split("n");

	InternetAddress[] mailAddress_BCC = new InternetAddress [bCCAddresses.length];

	for (int i=0; i

		String bccline = BCCAddresses[i];

		String bccdelims = "["+ (delimitor) +"]";

		String[] BCCParts = bccline.split(bccdelims);

		String BCCName = BCCParts [0];

		String BCCAddress = BCCParts [1];

	mailAddress_BCC[i] = new InternetAddress(BCCAddress, BCCName);

	}

   message.addRecipients(Message.RecipientType.BCC, mailAddress_BCC);

}



//-----------------------------------------------------------------

// add multipart alternatives (related=plain & html (& attachments), mixed = plain & attachments)



	MimeMultipart multipartRoot = new MimeMultipart("alternative");

	MimeBodyPart contentRoot = new MimeBodyPart();



//if(htmlText!=null) {

	MimeMultipart multipartAlt = new MimeMultipart("related");

//}

//if(attachments!=null && htmlText==null) {

//	MimeMultipart multipartAlt = new MimeMultipart("mixed");

//}



// add plain text message

if(htmlText==null && attachments==null) {

	message.setText(plainText, "ISO-8859-1");

} else {

	BodyPart messageBodyPart;

	messageBodyPart = new MimeBodyPart();

	if(plainText!=null) {

		messageBodyPart.setText(plainText,"ISO-8859-1");

	} else {

		messageBodyPart.setText("There is no plain Text part in this mail. Please update your mail client to view the message.","us-ascii");

	}

	messageBodyPart.updateHeaders();

	multipartRoot.addBodyPart(messageBodyPart);

}



// add html message message

if(htmlText!=null) {

	messageBodyPart = new MimeBodyPart();

	messageBodyPart.setContent(htmlText,"text/html");

	messageBodyPart.updateHeaders();

	multipartAlt.addBodyPart(messageBodyPart);

}



// add attachments

if(attachments!=null) {

	String attachment=(attachments); 

	String[] attachmentLine = attachment.split("n") ;

	for (int i=0; i

		String aline = attachmentLine[i];

		String adelims = "["+ (delimitor) +"]";

		String[] aParts = aline.split(adelims);

		String aName = aParts [0];

		String aPath = aParts [1];

		messageBodyPart = new MimeBodyPart();

    	FileDataSource fileDataSource = new FileDataSource(aPath);

    	messageBodyPart.setDataHandler(new DataHandler(fileDataSource));

    	messageBodyPart.setFileName(aName);

    	messageBodyPart.setHeader("Content-Disposition","inline; filename=" + aName);

    	messageBodyPart.setHeader("Content-ID","<"+ aName +">");

    	multipartAlt.addBodyPart(messageBodyPart);	

	}

}



//--------------------------------------------------------------------

// set Hierarchy (only, if html or attachments are there, not for plain text messages

if(htmlText!=null || attachments!=null) {

    	contentRoot.setContent(multipartAlt);

    	multipartRoot.addBodyPart(contentRoot);

    	message.setContent(multipartRoot);

}



//-------------------------------------------------------------------

// Send with authentication

if(username!=null && password!=null) {

	Transport transport = Session.getDefaultInstance(props).getTransport("smtp");

	int port = Integer.valueOf(portNumber);

	transport.connect(smtpHost, port, username, password);

	transport.sendMessage(message, message.getAllRecipients());

	transport.close();

} else {

	transport.connect(smtpHost);

	Transport.send(message);

}

return true" )

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.