Send a mail with attachment in java - jakarta-mail

I want to send mail with attachment in java with the following criteria -
The file which I want to attach is a downloadable URL
(like : http://berkeleycollege.edu/browser_check/samples/excel.xls)
Code is deployed on appengine, so Write to File, Create File, Save File is prohibited by google.
I don't even have the permission to download the file first and attach it as an attachment.
I have tried InputStream input = new URL(url).openStream(); to read the file content pass that to a datahandler. Where url is mentioned above. Sample Code:
Message msg = new MimeMessage(session);
Multipart multiPart = new MimeMultipart();
MimeBodyPart attachment = new MimeBodyPart();
DataHandler handler;
InputStream input = new URL(url).openStream();
handler = new DataHandler(new ByteArrayDataSource(input,"xls");
attachment.setDataHandler(handler);
multiPart.addBodyPart(attachment);
msg.setContent(multiPart);
Transport.send(msg);
Above code is giving Invalid Content error.
Is there anyway, I can read the content of that downloadable link and that content can be send as attachment ?
Please share the code snippet for the reference.

Map model = new HashMap();
String text = null;
Properties properties_mail = new Properties();
InputStream iStream_mail = null;
String propFileName_mail = "properties/mail.properties";
InputStream stream_mail = getClass().getClassLoader().getResourceAsStream(propFileName_mail);
properties_mail.load(stream_mail);
MimeMessage message = this.javamailsenderImpl.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_RELATED, "UTF-8");
mimeMessageHelper.setFrom(properties_mail.getProperty("javaMailSender.username"));
mimeMessageHelper.setTo(user.getUserEmail());
mimeMessageHelper.setSubject("Bpa Qa Product - New Organisation User");
/*String mailBody = "Welcome New User!!! <br /> Your Login Id is : "+user.getUserEmail()+"Your Password is:"+user.getUserPassword();
mimeMessageHelper.setText(mailBody, true);*/
model.put("firstName", user.getFirstName());
model.put("userEmail", user.getUserEmail());
model.put("userPassword", user.getUserPassword());
model.put("organizationName", user.getOrganization().getOrganizationName());
model.put("heading", "Thank You for Registering with us .!! Here is your Login credentials.");
text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "UserDetails.vm", "UTF-8", model);
mimeMessageHelper.setText(new String(text.getBytes(), "UTF-8"), true);
this.javamailsenderImpl.send(message);

Related

Getting exception while sending email with attachment using AWS SES java SDK

I am trying to send email with an excel attachment using AWS SES java SDK. I am following the code template provided by AWS.
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-raw-using-sdk.html
But I am getting javax.mail.internet.ParseException. Any idea what's going on here ?
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Properties;
//JavaMail libraries. Download the JavaMail API
//from https://javaee.github.io/javamail/
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import org.apache.log4j.Logger;
//AWS SDK libraries. Download the AWS SDK for Java
//from https://aws.amazon.com/sdk-for-java
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.RawMessage;
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
public class AmazonSESSample {
private final Logger logger = Logger.getLogger(getClass());
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
private static String SENDER = "US CWB INTDEV <uscwbintdev#gmail.com>";
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
private static String RECIPIENT = "bhapanda#acme.com";
// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// ConfigurationSetName=CONFIGURATION_SET argument below.
private static String CONFIGURATION_SET = "ConfigSet";
// The subject line for the email.
private static String SUBJECT = "Weekly users and projects report";
// The email body for recipients with non-HTML email clients.
private static String BODY_TEXT = "Hello,\r\n" + "Please see the attached file for a list "
+ "of customers to contact.";
// The HTML body of the email.
private static String BODY_HTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
+ "<p>Please see the attached file for a " + "list of customers to contact.</p>" + "</body>" + "</html>";
public void send(byte[] attachment) throws AddressException, MessagingException, IOException {
Session session = Session.getDefaultInstance(new Properties());
// Create a new MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Add subject, from and to lines.
message.setSubject(SUBJECT, "UTF-8");
message.setFrom(new InternetAddress(SENDER));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(RECIPIENT));
// Create a multipart/alternative child container.
MimeMultipart msg_body = new MimeMultipart("alternative");
// Create a wrapper for the HTML and text parts.
MimeBodyPart wrap = new MimeBodyPart();
// Define the text part.
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(BODY_TEXT, "text/plain; charset=UTF-8");
// Define the HTML part.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(BODY_HTML, "text/html; charset=UTF-8");
// Add the text and HTML parts to the child container.
msg_body.addBodyPart(textPart);
msg_body.addBodyPart(htmlPart);
// Add the child container to the wrapper object.
wrap.setContent(msg_body);
// Create a multipart/mixed parent container.
MimeMultipart msg = new MimeMultipart("mixed");
// Add the parent container to the message.
message.setContent(msg);
// Add the multipart/alternative part to the message.
msg.addBodyPart(wrap);
// Define the attachment
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(attachment, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
att.setDataHandler(new DataHandler(fds));
att.setFileName(fds.getName());
// Add the attachment to the message.
msg.addBodyPart(att);
// Try to send the email.
try {
System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service
// call with the supplied AWS credentials.
AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
// Replace US_WEST_2 with the AWS Region you're using for
// Amazon SES.
.withRegion(Regions.US_WEST_2).build();
// Print the raw email content on the console
// Send the email.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage)
.withConfigurationSetName(CONFIGURATION_SET);
client.sendRawEmail(rawEmailRequest);
System.out.println("Email sent!");
// Display an error if something goes wrong.
} catch (Exception ex) {
System.out.println("Email Failed");
System.err.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
}
}
Here is the stack trace.
2019-01-10 09:57:52 <2a7abff3-14be-11e9-9b09-dbd922fa0afd> DEBUG AmazonWebServiceClient:79 - Internal logging successfully configured to commons logger: true
Email Failed
Error message: Expected parameter value, got "null"
javax.mail.internet.ParseException: Expected parameter value, got "null"
at javax.mail.internet.ParameterList.<init>(ParameterList.java:169)
at javax.mail.internet.ContentDisposition.<init>(ContentDisposition.java:87)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1307)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1001)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:333)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1255)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2012)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1980)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1680)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1659)
at com.acme.workbench.project.list.email.service.AmazonSESSample.send(AmazonSESSample.java:131)
at com.acme.workbench.project.list.email.service.ProjectListEmailService.execute(ProjectListEmailService.java:60)
at com.acme.lambda.handler.LambdaFunctionForScheduledEvent.execute(LambdaFunctionForScheduledEvent.java:68)
at com.acme.lambda.handler.LambdaFunctionForScheduledEvent.handleRequest(LambdaFunctionForScheduledEvent.java:55)
at com.acme.lambda.handler.LambdaFunctionForScheduledEvent.handleRequest(LambdaFunctionForScheduledEvent.java:13)
at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
You copied code that set the attachment from a file and changed it to set the attachment from a byte array, but the ByteArrayDataSource has no name so when you set the file name for the attachment you set it to null, which is what causes the problem. Change the call to att.setFileName to set a non-null file name.

How can I convert a filled iText PDF template into an Input Stream?

I'm trying to use an existing PDF template and iText to fill in the document, then send the file to our database.
However, I cannot figure out how to convert the finished iText PDF into a usable form - I can display it to the user easily enough, but I cannot get it into a File, InputStream, or even byte[] format to upload to our Database.
public ActionForward doIt(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
{
int docid = Integer.parseInt(form.getDocumentTemplateId());
byte[] byteTemplate = TemplateDb.getTemplate(docId);
PdfReader pdfReader = new PdfReader(byteTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, response.getOutputStream());
AcroFields acroFields = pdfStamper.getAcroFields();
acroFields.setField(//And then I set my acro fields, which works fine);
ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteTemplate );
// This is me calling a separate function to upload the Input Stream - but all that the inByteStream object contains is a blank template
DocumentManager.uploadDocument(inByteStream);
pdfStamper.close();
pdfReader.close();
}

How to test Android toast messages in Appium ( selenium Java)

I am using Selenium with Java to run scripts on android (thru Appium server).
I see that it is not possible to locate a toast by using selenium's
driver.findElement(By.LinkText("User not logged in")
in Appium
But can be used in Selendroid to capture toast messages.
I there a way I can use both Selendroid and Appium in the same script?
Finally, we are able to read the toast message without the need of taking screenshots and performing OCR.
I have tested this on Appium 1.15.1.
Toast messages comes under com.package.system.
Normally, Xpath for this will be "/hierarchy/android.widget.Toast".
And, Class Name will be "android.widget.settings"
You can confirm this by refreshing element inspector screen when toast message is displayed.
WebDriverWait waitForToast = new WebDriverWait(driver.25);
waitForToast.until(ExpectedConditions.presenceOfElementLoacted(By.xpath("/hierarchy/android.widget.Toast")));
String toastMessage = driver.findElement((By.xpath("/hierarchy/android.widget.Toast")).getText();
System.out.println(toastMessage);
Method 1: from Appium version 1.6.4 supports toast messages, for that you need to use automationName:'uiautomator2'.
toast = driver.find_element(:xpath, "//android.widget.Toast[1]")
if toast.text == "Hello"
But i don't recommend this because uiautomator2 is not stable yet.
Method 2:
Trigger text message on the screen
Capture screenshots
Convert image to text file
def assettoast(string)
sname = (0...8).map { (65 + rand(26)).chr }.join
$driver.driver.save_screenshot("#{sname}")
# Make sure tesseract is installed in the system. If not you can install using "brew install tesseract" in mac
system ("tesseract #{sname} #{sname}")
text_file="#{sname}.txt"
var= get_string_from_file(string, text_file)
raise if var != true
end
Check whether toast message is there in text file
def get_string_from_file(word, filename)
File.readlines(filename).each do |line|
return true if line.include?(word)
end
end
Looks like you can't switch driver type within same session.
If you trying to switch to Selendroid only for toast verification - you could use OSR image recognition engine.
Check this answer w/ Ruby bindings
Idea is quite simple:
make toast message to appear
take few screenshots
iterate over taken screenshots and look for required text
Here is nice and simple example of OCR usage in Java: tess4j example (make sure that Tesseract engine installed)
Step 1:
File scrFile=null;
String path1 = null;
BufferedImage originalImage=null;
BufferedImage resizedImage=null;
System.out.println("Starting\n\n\n\n");
scrFile = ((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.FILE);
System.out.println("after scrfile\n\n\n\n");
originalImage = ImageIO.read(scrFile);
System.out.println("after originalFile\n\n\n");
BufferedImage.TYPE_INT_ARGB : originalImage.getType();
resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
Step 2:
BufferedImage pathforToast= original image;
Step 3:
System.setProperty("jna.library.path","C:/Users/Dell/workspace/MOBILEFRAMEWORK/dlls/x64/");
Tesseract instance = Tesseract.getInstance();
`enter code here`ImageIO.scanForPlugins();
String result=null;
result = instance.doOCR(pathforToast);`enter code here`
System.out.println(result);`enter code here`
Appium 1.6.4#beta latest version supports toast messages
Take screen shot of Toast Message page and try to convert the image file in to Text and verify the text using the below code.
public void imageconversion(String filePath) throws IOException,
{
ITesseract instance = new Tesseract();
//file path is the image which you need to convert to text
File imageFile = new File(filePath);
BufferedImage img = null;
img = ImageIO.read(imageFile);
BufferedImage blackNWhite = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(img, 0, 0, null);
//path where your downloaded tessdata exists
instance.setDatapath("E://ocr//data");
//What language you required to convert,( e.g. English)
instance.setLanguage("eng");
String result = instance.doOCR(blackNWhite);
System.out.println(result);
}
Appium Directly does not give any API to read toast message we need to do it using tess4j jar. First we need to take screen shot and then we need to read the text from screen shot using tess4j API.
static String scrShotDir = "screenshots";
File scrFile;
static File scrShotDirPath = new java.io.File("./"+ scrShotDir+ "//");
String destFile;
static AndroidDriver driver = null;
public String readToastMessage() throws TesseractException {
String imgName = takeScreenShot();
String result = null;
File imageFile = new File(scrShotDirPath, imgName);
System.out.println("Image name is :" + imageFile.toString());
ITesseract instance = new Tesseract();
File tessDataFolder = LoadLibs.extractTessResources("tessdata"); // Extracts
// Tessdata
// folder
// from
// referenced
// tess4j
// jar
// for
// language
// support
instance.setDatapath(tessDataFolder.getAbsolutePath()); // sets tessData
// path
result = instance.doOCR(imageFile);
System.out.println(result);
return result;
}
/**
* Takes screenshot of active screen
*
* #return ImageFileName
*/
public String takeScreenShot() {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
new File(scrShotDir).mkdirs(); // Create folder under project with name
// "screenshots" if doesn't exist
destFile = dateFormat.format(new Date()) + ".png"; // Set file name
// using current
// date time.
try {
FileUtils.copyFile(scrFile, new File(scrShotDir + "/" + destFile)); // Copy
// paste
// file
// at
// destination
// folder
// location
} catch (IOException e) {
System.out.println("Image not transfered to screenshot folder");
e.printStackTrace();
}
return destFile;
}
For more details Refer this video - https://www.youtube.com/watch?v=lM6-ZFXiSls
I found three ways to capture a Toast message and verify them.
To get the page source and verify the toast message from it.
public void verifyToastMessageUsingPageSource(String toastmsg) throws InterruptedException {
boolean found = false;
for(int i =0 ; i <8; i++){
if(getDriver().getPageSource().contains("class=\"android.widget.Toast\" text=\""+toastmsg+"\"")){
found = true;
break;
}
Thread.sleep(300);
}
Assert.assertTrue(found,"toast message "+toastmsg+" is present");
}
Similar can be found using Xpath: //android.widget.Toast[1]
Using the grep command, wait for a toast message in uiautomator events.
run the command before clicking and toast message will be varified.
adb shell uiautomator events | grep "ToastMessgae"
This is tricky and needs more code to run.
start capturing screenshots thread.
perform click action
stop screen capturing thread.
extract text from the captured images using OCR and verify the toast message is present in the captured images.
I prefer the 1st and 2nd option, it provides validation in less time with less code.
comment if you need code for 2nd and 3rd point.
Appium with version number>=1.6.4 supports toast notification with UiAutomator2.
In Javascript with webdriver you can do like this
let toast=await driver1.elements("xpath","/hierarchy/android.widget.Toast");
let data=await toast[0].text();
console.log(data)

JAVA validate an image and if validated upload aws

I want to validate an uploaded image, that file is an image or not.
For this I am using Tika Parser, it parses it correctly but I get input stream as empty after parsing.
public static FileType checkFileType(InputStream inputStream) throws IOException, SAXException, TikaException,
InvalidArgumentException {
ContentHandler contenthandler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
ParseContext context = new ParseContext();
parser.parse(inputStream, contenthandler, metadata, context);
//inputStream.close();
String contentType = metadata.get(Metadata.CONTENT_TYPE);
}
I am getting input stream from org.apache.cxf.jaxrs.ext.multipart.Attachment
InputStream inputStream = attachment.getDataHandler().getInputStream();

FileUpload in wpf ( Access to the path is denied)

public void Uploader(string filename, Stream Data)
{
BinaryReader reader = new BinaryReader(Data);
string path = #"C:/Friendisc/Images";
FileStream fstream = new FileStream(path, FileMode.CreateNew);
BinaryWriter wr = new BinaryWriter(fstream);
wr.Write(reader.ReadBytes((int)Data.Length));
wr.Close();
fstream.Close();
Data.Close();
}
I am getting the error: Access to the path is denied.
What do I need to do?
Also How would I upload the image on another project within the same solution?
File path was not proper. Issue Resolved.

Resources