API JavaMail ialah rangka kerja yang teguh, bebas platform dan bebas protokol yang direka untuk menyokong aplikasi klien Java dalam melaksanakan fungsi e-mel dan pemesejan yang komprehensif. API ini menawarkan antara muka generik dengan kelas abstrak yang mewakili pelbagai objek yang dicipta dalam sistem e-mel transaksi. Ia amat berguna untuk aplikasi peringkat perusahaan di mana kebolehpercayaan dan kefungsian yang meluas adalah penting.
Berstruktur Baik dan Digunapakai Secara meluas:
Fungsi Serbaguna:
Integrasi Programatik:
Masa Kompilasi Lanjutan:
Penggunaan Memori:
Properties properties = new Properties(); Session session = Session.getDefaultInstance(properties, null);
Properties properties = new Properties(); Session session = Session.getInstance(properties, null);
Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("sender@gmail.com", "password"); } });
MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the email subject"); message.setText("This is the email body");
Multipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("This is the email body"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource("path/to/attachment.txt"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName("attachment.txt"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart);
Transport.send(message);
import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendMail { public static void main(String[] args) { String to = "receiver@gmail.com"; String from = "sender@gmail.com"; String host = "smtp.gmail.com"; Properties properties = System.getProperties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "465"); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true"); Session session = Session.getInstance(properties, new javax.mail.Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("sender@gmail.com", "password"); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Your email subject goes here"); Multipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("You have a new message"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource("path/to/attachment.txt"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName("attachment.txt"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); } catch (MessagingException mex) { mex.printStackTrace(); } } }
Simple Java Mail ialah perpustakaan mel mesra pengguna yang direka untuk memudahkan proses penghantaran e-mel SMTP dalam Java. Ia berfungsi sebagai pembalut di sekeliling API JavaMail, memperkemas proses penghantaran e-mel dengan mengurangkan kerumitan API asas.
Teguh dan Ringan:
Pematuhan RFC:
Sokongan Proksi SOCKS yang Disahkan:
Ciri Terperinci:
Email email = EmailBuilder.startingBlank() .from("From", "from@example.com") .to("1st Receiver", "rec1@example.com") .to("2nd Receiver", "rec2@example.com") .withSubject("Enhanced Email with HTML and Attachments") .withHTMLText("<html><body><h1>Hello!</h1><p>This is an enhanced email with HTML content.</p></body></html>") .withAttachment("path/to/attachment.txt") .buildEmail();
Mailer mailer = MailerBuilder .withSMTPServer("smtp.mailtrap.io", 2525, "username", "password") .withTransportStrategy(TransportStrategy.SMTPS) .buildMailer();
mailer.sendMail(email);
import org.simplejavamail.api.email.Email; import org.simplejavamail.email.EmailBuilder; import org.simplejavamail.mailer.Mailer; import org.simplejavamail.mailer.MailerBuilder; import org.simplejavamail.api.mailer.config.TransportStrategy; public class SendEnhancedMail { public static void main(String[] args) { Email email = EmailBuilder.startingBlank() .from("From", "from@example.com") .to("1st Receiver", "case1@example.com") .to("2nd Receiver", "case2@example.com") .withSubject("Enhanced Email with HTML and Attachments") .withHTMLText("<html><body><h1>Hello!</h1><p>This is an enhanced email with HTML content.</p></body></html>") .withAttachment("path/to/attachment.txt") .buildEmail(); Mailer mailer = MailerBuilder .withSMTPServer("smtp.mailtrap.io", 2525, "username", "password") .withTransportStrategy(TransportStrategy.SMTPS) .buildMailer(); mailer.sendMail(email); } }
SuprSend menawarkan infrastruktur pemberitahuan berbilang saluran pihak ketiga yang komprehensif yang menyokong penghantaran pemberitahuan merentas pelbagai saluran seperti e-mel, SMS dan pemberitahuan tolak melalui API bersatu. Dengan memanfaatkan SuprSend, pembangun boleh mengurus aliran kerja pemberitahuan yang kompleks dengan lancar.
Pilihan Penyepaduan Luas:
Tiada Ketergantungan Teknologi:
Intelligent Routing:
In-App SDK:
Granular Template Management:
Powerful Workspace:
Unified Analytics:
Smart Automation:
Scalability:
Limits:**
import com.suprsend.Notification; import com.suprsend.NotificationBuilder; import com.suprsend.SuprSendClient; public class SendNotification { public static void main(String[] args) { // Initialize the SuprSendClient with API key and Workspace Secret SuprSendClient client = new SuprSendClient("your_api_key", "your_workspace_secret"); // Build the notification Notification notification = NotificationBuilder.startingBlank() .withRecipientEmail("recipient@example.com") .withRecipientSMS("recipient_phone_number") .withSubject("Notification Subject") .withHTMLBody("<html><body><h1>Hello!</h1><p>This is a multichannel notification.</p></body></html>") .build(); // Send the notification client.sendNotification(notification); } }
import com.suprsend.Notification; import com.suprsend.NotificationBuilder; import com.suprsend.SuprSendClient; public class SuprSendExample { public static void main(String[] args) { // Initialize the SuprSendClient with API key and Workspace Secret SuprSendClient client = new SuprSendClient("your_api_key", "your_workspace_secret"); // Create the notification Notification notification = NotificationBuilder.startingBlank() .withRecipientEmail("receiver@example.com") .withSubject("Subject of the Notification") .withHTMLBody("<html><body><h1>Hello!</h1><p>This is a notification from SuprSend.</p></body></html>") .withAttachment("path/to/attachment.txt") .build(); // Send the notification client.sendNotification(notification); } }
These methods offer a comprehensive guide to sending email notifications using Java, with varying levels of complexity and integration capabilities to suit different needs and scenarios.
You may want to check out other SuprSend SDKs too. Consider giving us a star after usage. It's free and open.
SuprSend Go SDK
go get github.com/suprsend/suprsend-go
Initialize the SuprSend SDK
import (
"log"
suprsend "github.com/suprsend/suprsend-go"
)
func main() {
opts := []suprsend.ClientOption{
// suprsend.WithDebug(true),
}
suprClient, err := suprsend.NewClient("__api_key__", "__api_secret__", opts...)
if err != nil {
log.Println(err)
}
}
package main
import (
"log"
suprsend "github.com/suprsend/suprsend-go"
)
func main() {
// Instantiate Client
suprClient, err := suprsend.NewClient("__api_key__", "__api_secret__")
if err != nil {
log.Println(err)
return
}
// Create workflow body
wfBody := map[string]interface{}{
"name": "Workflow Name",
"template": "template slug",
"notification_category": "category",
// "delay": "15m", // Chek duration format in documentation
"users": []map[string]interface{}{
{
"distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
…This package can be included in a python3 project to easily integrate with SuprSend platform.
We're working towards creating SDK in other languages as well.
suprsend-py-sdk is available on PyPI. You can install using pip.
pip install suprsend-py-sdk
This SDK depends on a system package called libmagic. You can install it as follows:
<span class="pl-c"># On debian based systems</span>
sudo apt install libmagic
<span class="pl-c"># If you are using macOS</span>
brew install libmagic
Initialize the SuprSend SDK
from suprsend import Suprsend
# Initialize SDK
supr_client = Suprsend("workspace_key", "workspace_secret")
Following example shows a sample request for triggering a workflow. It triggers a notification to a user with id: distinct_id, email: user@example.com & androidpush(fcm-token): __android_push_fcm_token__ using template purchase-made and notification_category system
from suprsend import Workflow
…This package can be included in a node project to easily integrate with SuprSend platform.
npm install @suprsend/node-sdk@latest
const { Suprsend } = require("@suprsend/node-sdk");
const supr_client = new Suprsend("workspace_key", "workspace_secret");
It is a unified API to trigger workflow and doesn't require user creation before hand. If you are using our frontend SDK's to configure notifications and passing events and user properties from third-party data platforms like Segment, then event-based trigger would be a better choice.
const { Suprsend, WorkflowTriggerRequest } = require("@suprsend/node-sdk");
const supr_client = new Suprsend("workspace_key", "workspace_secret");
// workflow payload
const body = {
workflow: "_workflow_slug_",
actor: {
distinct_id: "0fxxx8f74-xxxx-41c5-8752-xxxcb6911fb08",
name: "actor_1",
},
recipients: [
{
distinct_id: "0gxxx9f14-xxxx-23c5-1902-xxxcb6912ab09",
$email: ["abc@example.com"
…Integrating SuprSend Inbox channel in React websites can be done in two ways:
Detailed documentation can be found here: https://docs.suprsend.com/docs/inbox-react
You can install SuprSend inbox SDK using npm/yarn
npm install @suprsend/react-inbox
After installing, Import the component in your code and use it as given below. Replace the variables with actual values.
import SuprSendInbox from '@suprsend/react-inbox'
import 'react-toastify/dist/ReactToastify.css' // needed for toast notifications, can be ignored if hideToast=true
// add to your react component;
<SuprSendInbox
workspaceKey='<workspace_key>'
subscriberId='<subscriber_id>'
distinctId='<distinct_id>'
/>
interface ISuprSendInbox {
workspaceKey: string
distinctId: string | null
subscriberId: string | null
tenantId?: string
stores?: IStore[]
pageSize?: number
pagination?: boolean
…Atas ialah kandungan terperinci Hantar e-mel menggunakan Java (API Javamail atau Mel Java Mudah atau SuprSend Java SDK). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!