3

I am using dependency https://pub.dev/packages/mailer to send auto message through flutter web application for this part I am using the following code.

String username="************";
                  String password = "*******";
                  var smtpServer = gmail(username,password);
                  final message = Message()
                    ..from = Address(username, 'Truck App')
                    ..recipients.add('******')
                    ..subject = 'Testing'
                    ..text = msg.text
                    ..html = "<h1>Test</h1>\n<p> Hey! Here\'s some text</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}');
                    }
                  }
                  final equivalentMessage = Message()
                    ..from = Address(username, 'Your name ')
                    ..recipients.add(Address('******'))
                    ..ccRecipients.addAll([Address('******')])
                    ..bccRecipients.add('*********')
                    ..subject = 'Test Dart Mailer library ::  ::'
                    ..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>';

                  final sendReport2 = await send(equivalentMessage, smtpServer);
                  var connection = PersistentConnection(smtpServer);
                  await connection.send(message);
                  await connection.send(equivalentMessage);
                  await connection.close();

but I am getting the error like this:

js_helper.dart:1130 Uncaught Unsupported operation: Socket constructor
    at Object.b (http://localhost:52881/main.dart.js:4220:3)
    at Object.aVb (http://localhost:52881/main.dart.js:8007:31)
    at Object.aVc (http://localhost:52881/main.dart.js:8034:5)
    at http://localhost:52881/main.dart.js:79815:14
    at awO.a (http://localhost:52881/main.dart.js:5925:71)
    at awO.$2 (http://localhost:52881/main.dart.js:38402:23)
    at Object.W (http://localhost:52881/main.dart.js:5911:19)
    at Ku.yv (http://localhost:52881/main.dart.js:79822:10)
    at http://localhost:52881/main.dart.js:25197:14
    at awO.a (http://localhost:52881/main.dart.js:5925:71)
yogender
  • 202
  • 3
  • 12

2 Answers2

3

Socket objects cannot be constructed on web, since they come from dart:io which is not available when compiling to JS.

When looking at a package on pub.dev, you will be able to see the supported platforms. If you go to https://pub.dev/packages/mailer, you will notice it only supports Dart native, and web is missing from the supported Flutter platforms

cameron1024
  • 9,083
  • 2
  • 16
  • 36
2

So here i got my answer to this question as mentioned in the earlier answe by cameron1024 this code is compactible and is working fine for android platform( tested) but will throw the same error when you will try to integrate it on web platform.

so to send the mails through web platform you can use sendgrid_mailer package available on pub.dev

to use sendgrid _mailer you need to have api key which can be available from sendgrid official website.

Thank you

yogender
  • 202
  • 3
  • 12