Created
June 24, 2021 18:45
-
-
Save SurajBahadur/458861fdc6835b0b57b43948322b3a7b to your computer and use it in GitHub Desktop.
Sending an email without opening the third party app such as Gmail etc in Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:mailer/mailer.dart'; | |
import 'package:mailer/smtp_server.dart'; | |
main() async { | |
String username = '[email protected]'; | |
String password = 'password'; | |
final smtpServer = gmail(username, password); | |
// Use the SmtpServer class to configure an SMTP server: | |
// final smtpServer = SmtpServer('smtp.domain.com'); | |
// See the named arguments of SmtpServer for further configuration | |
// options. | |
// Create our message. | |
final message = Message() | |
..from = Address(username, 'Your name') | |
..recipients.add('[email protected]') | |
..ccRecipients.addAll(['[email protected]', '[email protected]']) | |
..bccRecipients.add(Address('[email protected]')) | |
..subject = 'Test Dart Mailer library :: π :: ${DateTime.now()}' | |
..text = 'This is the plain text.\nThis is line 2 of the text part.' | |
..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>"; | |
try { | |
final sendReport = await send(message, smtpServer); | |
print('Message sent: ' + sendReport.toString()); | |
} on MailerException catch (e) { | |
print('Message not sent.'); | |
for (var p in e.problems) { | |
print('Problem: ${p.code}: ${p.msg}'); | |
} | |
} | |
// DONE | |
// Let's send another message using a slightly different syntax: | |
// | |
// Addresses without a name part can be set directly. | |
// For instance `..recipients.add('[email protected]')` | |
// If you want to display a name part you have to create an | |
// Address object: `new Address('[email protected]', 'Display name part')` | |
// Creating and adding an Address object without a name part | |
// `new Address('[email protected]')` is equivalent to | |
// adding the mail address as `String`. | |
final equivalentMessage = Message() | |
..from = Address(username, 'Your name π') | |
..recipients.add(Address('[email protected]')) | |
..ccRecipients.addAll([Address('[email protected]'), '[email protected]']) | |
..bccRecipients.add('[email protected]') | |
..subject = 'Test Dart Mailer library :: π :: ${DateTime.now()}' | |
..text = 'This is the plain text.\nThis is line 2 of the text part.' | |
..html = '<h1>Test</h1>\n<p>Hey! Here is some HTML content</p><img src="cid:[email protected]"/>' | |
..attachments = [ | |
FileAttachment(File('exploits_of_a_mom.png')) | |
..location = Location.inline | |
..cid = '<[email protected]>' | |
]; | |
final sendReport2 = await send(equivalentMessage, smtpServer); | |
// Sending multiple messages with the same connection | |
// | |
// Create a smtp client that will persist the connection | |
var connection = PersistentConnection(smtpServer); | |
// Send the first message | |
await connection.send(message); | |
// send the equivalent message | |
await connection.send(equivalentMessage); | |
// close the connection | |
await connection.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment