Java – Send EMail with Attachment

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

public class EmailAttachmentDemo {

	public static void main(String[] args) {
		EmailAttachmentDemo demo = new EmailAttachmentDemo();
		demo.sendEmail();
	}

	public void sendEmail() {
		String from = "email@example.com";
		String to = "email@example.com";
		String subject = "Important Message";
		String bodyText = "This is a important message with attachment";
		String filename = "jee-sample/src/main/resources/data.txt";

		Properties properties = new Properties();
		properties.put("mail.smtp.host", "smtp.example.com");
		properties.put("mail.smtp.port", "25");
		Session session = Session.getDefaultInstance(properties, null);

		try {
			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());

			//
			// Set the email message text.
			//
			MimeBodyPart messagePart = new MimeBodyPart();
			messagePart.setText(bodyText);

			//
			// Set the email attachment file
			//
			MimeBodyPart attachmentPart = new MimeBodyPart();
			FileDataSource fileDataSource = new FileDataSource(filename) {
				@Override
				public String getContentType() {
					return "application/octet-stream";
				}
			};
			attachmentPart.setDataHandler(new DataHandler(fileDataSource));
			attachmentPart.setFileName(fileDataSource.getName());

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

			message.setContent(multipart);

			Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}

Java Struts – Handle HTTP 404 Error

The “HTTP Status 404” error code shows that the system can not find the page or resource you requested. In practice, you should display your custom 404 error page. However, it is handle in web.xml , not in Struts framework. For example,

<web-app>
...
<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>

When system hit the 404 error, it will forward to your custom 404 error page “/pages/error404.jsp“.

struts-config.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Maven Struts Examples</display-name>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>

Java iText – Add Anchor / Link to PDF

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class AnchorDemo {

	public static void main(String[] args) {
		Document document = new Document();
		try {
			PdfWriter.getInstance(document,
					new FileOutputStream("AnchorDemo.pdf"));
			document.open();

			String content = "You can learn Java programming on the " + "following website: ";
			Paragraph paragraph = new Paragraph(content);

			//
			// Creates a new anchor that link to external website
			// and add this anchor to the paragraph.
			//
			Anchor anchor = new Anchor("javac - Java Examples Source Code");
			anchor.setReference("http://javac.in");
			paragraph.add(anchor);

			document.add(paragraph);
		} catch (DocumentException | FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
	}

}

Page 20 of 49« First...10...1819202122...3040...Last »