Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
Component Sharing
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Gagandeep Singh Bhatia
Component Sharing
Commits
cf22a492
Commit
cf22a492
authored
Oct 07, 2024
by
Gagandeep Singh Bhatia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ImageEmailSender CID Image Attachment Sender
parent
3ea3d9de
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
0 deletions
+98
-0
GSB/ImageEmailSender.java
GSB/ImageEmailSender.java
+98
-0
No files found.
GSB/ImageEmailSender.java
0 → 100644
View file @
cf22a492
package
test
;
import
java.io.File
;
import
java.util.Properties
;
import
javax.activation.DataHandler
;
import
javax.activation.DataSource
;
import
javax.activation.FileDataSource
;
import
javax.mail.Authenticator
;
import
javax.mail.BodyPart
;
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
;
public
class
ImageEmailSender
{
public
static
void
sendEmailWithEmbeddedImage
(
String
to
,
String
subject
,
String
body
,
String
imagePath
)
{
// Sender's email address and password
final
String
username
=
"gagandeep.bhatia@proteustech.in"
;
final
String
password
=
"Proteus@7878"
;
// SMTP properties
Properties
props
=
new
Properties
();
props
.
put
(
"mail.smtp.auth"
,
"true"
);
props
.
put
(
"mail.smtp.starttls.enable"
,
"true"
);
props
.
put
(
"mail.smtp.host"
,
"smtp.gmail.com"
);
props
.
put
(
"mail.smtp.port"
,
"587"
);
// Create a session with authentication
Session
session
=
Session
.
getInstance
(
props
,
new
Authenticator
()
{
protected
PasswordAuthentication
getPasswordAuthentication
()
{
return
new
PasswordAuthentication
(
username
,
password
);
}
});
try
{
// Create a new message
Message
message
=
new
MimeMessage
(
session
);
// Set From: header field
message
.
setFrom
(
new
InternetAddress
(
username
));
// Set To: header field
message
.
setRecipients
(
Message
.
RecipientType
.
TO
,
InternetAddress
.
parse
(
to
));
// Set Subject: header field
message
.
setSubject
(
subject
);
// Create a multipart message
Multipart
multipart
=
new
MimeMultipart
(
"related"
);
// First part (the html)
BodyPart
messageBodyPart
=
new
MimeBodyPart
();
String
htmlText
=
"<html><body>"
+
body
+
"<br><img src=\"cid:image\"></body></html>"
;
messageBodyPart
.
setContent
(
htmlText
,
"text/html"
);
multipart
.
addBodyPart
(
messageBodyPart
);
File
imageFile
=
new
File
(
imagePath
);
// Second part (the image)
messageBodyPart
=
new
MimeBodyPart
();
DataSource
fds
=
new
FileDataSource
(
imagePath
);
messageBodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
messageBodyPart
.
setHeader
(
"Content-ID"
,
"<image>"
);
messageBodyPart
.
setDisposition
(
MimeBodyPart
.
INLINE
);
messageBodyPart
.
setFileName
(
imageFile
.
getName
());
multipart
.
addBodyPart
(
messageBodyPart
);
// Set the content of the message
message
.
setContent
(
multipart
);
// Send the message
Transport
.
send
(
message
);
System
.
out
.
println
(
"Email sent successfully"
);
}
catch
(
MessagingException
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
main
(
String
[]
args
)
{
// String to = "kandarp.baghar@proteustech.in";
String
to
=
"kandarp.baghar@proteustech.in"
;
String
subject
=
"Test Email with Embedded Image"
;
String
body
=
"This is a test email with an embedded image:"
;
String
imagePath
=
"/home/gsb/Pictures/vision.png"
;
sendEmailWithEmbeddedImage
(
to
,
subject
,
body
,
imagePath
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment